Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-07-2010, 09:47 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 14
OblivianStudios is on a distinguished road
Default Moving UIImageView on Button Press

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
OblivianStudios is offline   Reply With Quote
Old 03-08-2010, 11:52 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

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:

Code:
-(IBAction) moveUp{
     //move up 10 pixels
     player.center = CGPointMake( player.center.x, player.center.y -10);
}
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.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-08-2010, 09:18 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 14
OblivianStudios is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
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:

Code:
-(IBAction) moveUp{
     //move up 10 pixels
     player.center = CGPointMake( player.center.x, player.center.y -10);
}
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:

imageMoveViewController.h
Code:
@interface moveImageTestViewController : UIViewController {
	
	IBOutlet UIImageView *image1;

}

- (IBAction)moveUp;
imageMoveViewController.m
Code:
- (IBAction)moveUp{

	image1.center = CGPointMake( image1.center.x, image1.center.y -10);
	
}
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?

Thanks

-Oblivian Studios
OblivianStudios is offline   Reply With Quote
Old 03-08-2010, 10:48 PM   #4 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 65
Abel95 is on a distinguished road
Default

Id like to know this too, When the user holds the button the image moves continuously?
How would i do that?
Abel95 is offline   Reply With Quote
Old 03-08-2010, 11:56 PM   #5 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

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.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-09-2010, 12:26 AM   #6 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 65
Abel95 is on a distinguished road
Default

So a timer for each button? then an action for when the timer starts?
and i dont get the 2nd part.
Abel95 is offline   Reply With Quote
Old 03-09-2010, 01:28 AM   #7 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by Abel95 View Post
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:
Code:
if(upPressed)
     player.center = CGPointMake( player.center.x, player.center.y -10);
Extra credit: it is possible to have a single IBAction that handles all four buttons and both press and release, but I wouldn't worry about that now.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-09-2010, 11:57 AM   #8 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 65
Abel95 is on a distinguished road
Default

Ok so upPressed is already built in the buttons so it recognizes wut it does?
Ill try now.
Abel95 is offline   Reply With Quote
Old 03-09-2010, 01:39 PM   #9 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by Abel95 View Post
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.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 06-15-2010, 03:59 PM   #10 (permalink)
Registered Member
 
Join Date: Jun 2010
Location: USA
Posts: 3
SammyD is on a distinguished road
Smile Thanks!

This helped me a lot. Thanks!
SammyD is offline   Reply With Quote
Old 06-22-2010, 08:24 PM   #11 (permalink)
Registered Member
 
Join Date: Jun 2010
Location: USA
Posts: 3
SammyD is on a distinguished road
Post

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!
SammyD is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 399
9 members and 390 guests
13dario13, iOS.Lover, JackReidy, jeroenkeij, Leslie80, Sami Gh, Wikiboo, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:45 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0