Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6084125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:29:05+00:00 2026-05-23T11:29:05+00:00

I am trying to make a sprite move to a point that is touched

  • 0

I am trying to make a sprite move to a point that is touched by the user. My problem is I declare the sprite inside my init like this:

CCSprite *ball = [CCSprite spriteWithFile:@"ball.png"                            
rect:CGRectMake(0, 0, 20, 20)];

and inside my touch event:

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

I reference the ball and it says it’s “undefined”. Due to my understanding of scope in other languages I would hypothesize that I need to make “ball” a global variable, right? I have read that Global variables are typically “frowned upon” in objective-C. Why is this so? Is it memory related?

If you suggest something could you please let me know if I need to deallocate it at the close of my app. I’m really a beginner in iOS development.

Any advice would be a huge help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:29:05+00:00Added an answer on May 23, 2026 at 11:29 am

    Global variables are not just frowned upon in Objective C, they are usually bad practice for most languages. It leads to poor program design.

    Generally speaking, if you want an object to create and continue to access another object throughout its lifetime and not merely within a single method, you should assign it to an instance variable.

    In this case, you appear to be using cocos2d – so it’s likely that your scene should have a ball instance variable. You should release it (you don’t call dealloc in Objective C) when your scene is deallocated, if not before.

    If you are shaky with knowing when to release it, you’ll probably want to read the Memory Management Programming Guide. For a start, if you want that instance variable to not crash your program, you’ll need to retain that sprite.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make a simple program to catalogue books. Something like this, for example:
I am trying to make a multi-textured point sprite for an iphone application using
I am trying to make a sprite follow a bezier curve. I found a
Trying to make a make generic select control that I can dynamically add elements
Trying to make a custom :confirm message for a rails form that returns data
I am trying to make a sprite animate in cocos2D . I believe I
I am trying to make a sprite dance according to the beat of a
I'm trying to make a Flex Tree Component display the children of a sprite
I'm trying to make a game where the user is supposed to drag a
I'm trying to learn how to make an animated sprite in android and couldn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.