I am trying to develop a game for the iPhone but I've run into a road block. I know how to move images with touches but I haven't been able to move images with a button. Say there are 4 buttons (up, down, left, right), I want to know how I can move my image when pressing the corresponding button.
Thanks
-Oblivian Studios
P.S. This is my first post in the form and I'm glad to be here
I assume you can get your button hooked up to an IBAction? And you have a pointer to the image (either an IBOutlet or a pointer you created in code?) Then you can change the UIImageView's position with this code:
The biggest hurdle here is that it *seems* like you should be able to just do this:
Code:
//this won't work
player.center.y = player.center.y - 10;
//or the equivalent
player.center.y -=10;
You'll get an error about lvalues, indicating a problem with the left-hand value in the assignment. The reason you can't do that is that ".center" is a property, but ".y" is a member of a struct. "player.center" is shorthand for either getting or setting the property, but it can't do both at the same time. If we translate the dot syntax into bracket syntax, we see the problem:
Code:
//this is why we can't set y directly
[player center].y = [player center].y - 10;
You see that [player center] is a getter method - it gives you the value of "center" so that you can get to the value of y. You can not use it to set the value of y. You can only set the entire "center" struct once, like I did in my code. This would not be an issue if center were an object and y were a property, but they're not.
Extra credit: The code I gave moves you up by 10 pixels every time you tap, so the player's speed depends on the speed that the user can tap and the how fast the system returns tap events. It might be better for you to set some boolean values instead, like upPressed, downPressed, leftPressed, rightPressed, to TRUE when you click the button, and FALSE when you release it. Then you can do the actual movement in your gameUpdate method triggered by a NSTimer, ensuring that the player's speed is whatever you want it to be.
I assume you can get your button hooked up to an IBAction? And you have a pointer to the image (either an IBOutlet or a pointer you created in code?) Then you can change the UIImageView's position with this code:
The biggest hurdle here is that it *seems* like you should be able to just do this:
Code:
//this won't work
player.center.y = player.center.y - 10;
//or the equivalent
player.center.y -=10;
You'll get an error about lvalues, indicating a problem with the left-hand value in the assignment. The reason you can't do that is that ".center" is a property, but ".y" is a member of a struct. "player.center" is shorthand for either getting or setting the property, but it can't do both at the same time. If we translate the dot syntax into bracket syntax, we see the problem:
Code:
//this is why we can't set y directly
[player center].y = [player center].y - 10;
You see that [player center] is a getter method - it gives you the value of "center" so that you can get to the value of y. You can not use it to set the value of y. You can only set the entire "center" struct once, like I did in my code. This would not be an issue if center were an object and y were a property, but they're not.
Extra credit: The code I gave moves you up by 10 pixels every time you tap, so the player's speed depends on the speed that the user can tap and the how fast the system returns tap events. It might be better for you to set some boolean values instead, like upPressed, downPressed, leftPressed, rightPressed, to TRUE when you click the button, and FALSE when you release it. Then you can do the actual movement in your gameUpdate method triggered by a NSTimer, ensuring that the player's speed is whatever you want it to be.
Thanks for the help.
I implemented the code you gave into my app and I have the following code:
That works but the user would have to keep pressing the "Up" button. Is there a way to make it so if the user holds the button the image will keep moving?
You need an NSTimer that calls a method (which you will write) called gameUpdate. That method will check if the variables upPressed, downPressed, leftPressed, or rightPressed are true, and move the player accordingly.
Then you need two actions for each button; one to set rightPressed to true when you press down, and another to set rightPressed = false when you release.
So a timer for each button? then an action for when the timer starts?
and i dont get the 2nd part.
No, you will have one timer, which calls the one gameUpdate method.
For each button you'll have a buttonPress and a buttonRelease IBAction. The buttonPress action for the "up" button should set the variable upPressed = true, the buttonRelease action should set upPressed = false.
Then in the gameUpdate method you can say things like:
Ok so upPressed is already built in the buttons so it recognizes wut it does?
Ill try now.
No, upPressed, downPressed, leftPressed, and rightPressed are boolean variables that you must create. They'll be instance variables, probably in your view controller.
After I hooked everything together, I got a couple errors, so I was wondering where do I define the button press action in the gameUpdate method. Can anyone help? Thanks!