moving UIImageView using UIButton - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: moving UIImageView using UIButton (/thread-8366.html)
moving UIImageView using UIButton - MDev - Dec 1, 2010 09:44 AM
I am new to this group and to programming.
Your help is greatly appreciated!
I am getting confused with trying to move a UIImageView when a UIButton is pressed. I am Using a CADisplayLink:
Then, create a BOOL thats says button is down:
Also, in the gameLoop see if the button is down to move the image:
It moves continuously regardless of whether the button is pressed or not. If you can help me understand how this would work, you will make my day!
RE: moving UIImageView using UIButton - ThemsAllTook - Dec 1, 2010 10:48 AM
In the code you posted, leftButtonDown is declared in the scope of the -leftButtonPressed: method, which wouldn't make it accessible from -gameLogic where you're reading it. If this is what's in your actual code, either it won't compile, or there's another definition of leftButtonDown somewhere.
Assuming leftButtonDown is declared as a single variable accessible to both methods, do you set it to NO on initialization and when the button is released?
RE: moving UIImageView using UIButton - MDev - Dec 1, 2010 02:28 PM
Thanks for your reply.
I get a few warnings that 'leftButtonDown is an unused variable'.
I've added leftButtonDone here:
and added an IBAction for when the button is released:
and changed the timer selector to 'gameLogic':
and now the UIImageView doesn't move.
P.S. Is there a better way for me to post code?
RE: moving UIImageView using UIButton - EvolPenguin - Dec 1, 2010 03:33 PM
Two things, there is a way to post code in a formatted code segment. If you make a new reply it should be the # button. Or you can type [ code ] and then [ /code ] without the spaces.
As for your issue, the variable leftButtonDown is still being declared in the local scope of the viewDidLoad method. Try declaring it in the header file (.h), and then modifying it in the implementation (.m) file.
RE: moving UIImageView using UIButton - MDev - Dec 1, 2010 03:53 PM
Thanks for taking the time to help me. It is very much appreciated.
This is in the .h file:
Is this what you meant?
To make the UIImageView to move, do I need to do something more in the gameLogic method?
Sorry about my rookieness
RE: moving UIImageView using UIButton - MDev - Dec 1, 2010 05:00 PM
I guess I should put it all it one place for clarity:
I have been struggling with this for a while, thanks!
RE: moving UIImageView using UIButton - ThemsAllTook - Dec 1, 2010 06:54 PM
The statement BOOL leftButtonDown = NO; in -viewDidLoad (and similar in other methods) declares (and initializes to NO) a new local variable named leftButtonDown, which is separate from the now-hidden leftButtonDown instance variable. This is perfectly valid in C, so it compiles and runs, but changing the local variable has no effect on the instance variable. If you remove "BOOL" from the statements where you're assigning to leftButtonDown, you'll be referencing the instance variable rather than declaring a new local variable of the same name, and your code should work.
RE: moving UIImageView using UIButton - MDev - Dec 1, 2010 09:30 PM
That is GREAT! Thank you so much!