iPhone Game Programming – Tutorial 11 – Joypad

At last, I’ve finally recorded the joypad tutorial. This is a short tutorial of 20 minutes that covers how to implement a joypad control into the code that has been used through the other tutorials.

Tutorial 11

Tutorial 11 Project

Update 19/12/09:

After a request I’ve created a very simple view based app that does the same as the OpenGL version in the tutorial video. They key difference is that this app does nothing but move the ship around the screen to demo the joypad code without the need for any of the other code from the tutorials. Hope it helps

JoypadExample Project

Mike

Share:
  • Digg
  • del.icio.us
  • Facebook
  • MySpace
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Design Float
  • DZone
  • email
  • Google Bookmarks
  • LinkedIn
  • Scoopeo
  • Tumblr

91 Comments

jjack  on December 17th, 2009

Thanks for finally making the tutorial but i have one question.
When you make a tile map how do you make one that is perfectly fitting to the iphone screen with 16×16 pixel images
Thanks

RoberRM  on December 17th, 2009

Thank you, Mike, for finding time to feed us another tasty tutorial. :D
Too bad it is on Vimeo and I cannot download it to view it when I can (usually when I do not have internet connection). :( Or is there a way to download videos from Vimeo?
Anyway, thank you very, very much for continue sharing your efforts with us. ;)

Memphis  on December 18th, 2009

Hi, Thank you so much for your great tutorial. keep posting….

Tweets that mention iPhone Game Programming – Tutorial 11 – Joypad | 71² - The ramblings of two 30-something developers -- Topsy.com  on December 18th, 2009

[...] This post was mentioned on Twitter by 71Squared, erin dylan. erin dylan said: iPhone Game Programming – Tutorial 11 – Joypad | 71² – The …: 71² – The ramblings of two 30-something dev… http://bit.ly/4JnxYL [...]

Scott  on December 18th, 2009

Thanks for putting this up for us Mike. Great work as usual. Merry Christmas!

-Scott

nexus6  on December 18th, 2009

@Mike
Another great tutorial, Have a happy holiday!!

PaulC  on December 18th, 2009

Thanks for this Mike. Much appreciated.

I’ve no use for the joystick itself for my game, but I was particularly interested in the hash identification for the touches. Upto now I’ve been identifying my touches by recording the last known position and then comparing it with [touch previousLocationInView]

Changing it to your hash method might fix the problem I mentioned in the forum where the simulator doesn’t release touches when the mouse drags off of the screen.

So yeah, thanks for the tutorial. Looking forward to the next one.

Merry Christmas.

Paul.

ZetaErre  on December 18th, 2009

Thank you Mike. Simple and clear.

Every time I run your code, my memory jump back in time to speccy games, Atic atac…

Merry xmas.

-Roberto

mike  on December 18th, 2009

Hi ZetaErre

There is a good reason for that and its because I’ve based my first iPhone game on AticAtac which was the very first computer game I ever owned :o)

With all the good memories I have of the game that got me into computers and computer games I could not resist :o)

Mike

9livesoftware  on December 19th, 2009

Hi,
I loved this tutorial but could you do one with only the code for the joypad because I’m trying to put it into my upcoming app but don’t know what part I need. Thanx in advance

mike  on December 19th, 2009

Hi 9livessoftware

I’ll see if I can pull a simple app together that uses the joypad. The only code you need is what you saw added to the .h file of the SettingsView.m file and then the contents of the three touches methods.

If you are writing code inside a view then you can place the code from the methods in the tutorial into the methods that the view would use when touches happen i.e. touchesBegan, touchesMoved and touchesEnded.

In the tutorial I am passing touches caught in this way in the EAGLView into the current scene where my methods then handle the touches.

You should just be able to lift the code from those three methods, drop them into the methods mentioned above and then wire the resulting joypad movement into your game.

Hope that helps.

Mike

9livesoftware  on December 19th, 2009

Thank you so much and I was wondering if I could give a shoutout to your website on my youtube channel?

mike  on December 19th, 2009

Your more than welcome and I’m happen for 71Squared to get mentions :o)

Mike

mike  on December 19th, 2009

I’ve just created a small View based app that implements the joypad code and nothing else. This should give you all you need to work out what is needed inside your app.

http://www.71squared.com/iPhone/JoypadExample.zip

Good luck and let me know how you get on.

Mike

9livesoftware  on December 19th, 2009

You are my hero. Sorry but I have one more question. How do you make change the speed of the ship when you use the joypad thanx in advance

mike  on December 19th, 2009

No problem. The speed is controlled by the distance from the center of the joypad to where you are touching. The further you move your finger from the center of the joypad the larger the distance value becomes.

I then device that value by 4, just to reduce the number a little and multiply the result by the angle at which I want the player to move.

Hope that makes sense.

Mike

9livesoftware  on December 19th, 2009

im so sorry but when i tried putting the code from the code you gave me into my app but the joypad cap won’t move. Here is the app can you tell me what i did wrong http://parrish.homeip.net:8080/Public/Bumper.zip

9livesoftware  on December 19th, 2009

Did u figure out what I did Wrong?

j0egas  on December 20th, 2009

Hey! great tutorial but one problem!I made it so when the ship colides with another image, it restarts. i can make the joystick go back, but i cant make the actual “ship” go back. When i try to do that, it goes back, but then flies up write away! would you be able to help

mike  on December 20th, 2009

Hi J0egas, if you can send me the code I’ll take a look.

9livessoftware, I’ve had a quick look, but not seen immediately what the problem is. If I get time I’ll look some more, but I’m not sure when that will be.

Mike

j0egas  on December 20th, 2009

Thanks for replying mike! this is the code i used:

joypad = CGRectMake(40, 400, 150, 150);
joypadCenterx = 54;
joypadCentery = 409;
joypadMaxRadius = 50;

// Game timer that is used to update the position of the ship 60fps
gameTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:@selector(mainGameLoop) userInfo:nil repeats:YES];

joypadCap.center = CGPointMake(joypadCenterx, joypadCentery);

myShipImage.center = CGPointMake(118, 312);

CGPoint newLocation = CGPointMake(myShipImage.center.x – distance/500 * cosf(touchAngle),
myShipImage.center.y – distance/500 * sinf(touchAngle));

myShipImage.center = newLocation;

9livesoftware  on December 20th, 2009

Thank you so much you rock!!!

9livesoftware  on December 20th, 2009

Please check out this vid when you have a chance
http://www.youtube.com/watch?v=JOkMTk6JO1w

9livesoftware  on December 20th, 2009

Any progress with the app?

jjack  on December 21st, 2009

I still dont understand the tile map sizes how do you make them so they perfectly fit the iphone screen

mike  on December 21st, 2009

Hi jjack

I’m not sure I completely understand the problem you are having, but with 16×16 files you will need 20×30 to fill the iPhone screen. When creating your tilemap in Tiled you would specify an overall map size of something like 200×200 and then when rendering a map layer you would use a width of say 22 and height of say 32.

The extra two tiles on the width and height would allow you to scroll the tilemap without seeing the edge tiles get replaced with the next row or column when you have moved a complete tile width/height.

I hope that makes some sense. If not, let me have some more details of the problem you are having.

Mike

jjack  on December 21st, 2009

like you know how in any image manipulation program their is an area where you can specitfy the size in pixels or inches. How do you know the size of the tile maps because i just want to make one that perfectly fits the iphone screen with tiles that are 16×16 pixels
Thanks

mike  on December 21st, 2009

OK, so if you wanted a tile map that just filled the screen and no more it would be 20 x 30 tiles. 20 x 16 = 320 pixels and 30 x 16 = 480 pixels.

You can do the same calculation for any size of tile map, so a map of 65 tiles wide would be 65×16 = 1040.

Does that help?

Mike

jjack  on December 21st, 2009

How do you then just render it like an image becasue i dont want it to move

jjack  on December 22nd, 2009

how do you render it as one big opaque texture…

mike  on December 22nd, 2009

Hi jjack. All you need to do is work on which tiles you want to render from the tilemap i.e. an x and y location in the map and then how many tiles wide and high you want to render. This information is provided to the render method for the Tiled map class. that will then render those tiles to the screen.

The other piece of info you will need to provide is where on the screen you want the rendering to start from.

Mike

9livesoftware  on December 22nd, 2009

Did you figure out what I did wrong with my app?

mike  on December 22nd, 2009

Hi 9lives

I’m afraid I’ve not had a chance to really look at it further. Been mad busy with work and trying to get Sir Lamorak’s Quest finished.

If I get a chance later I’ll take another look, just not enough hours in a day :o(

Mike

mcgoogles  on December 22nd, 2009

Hey Mike,

I’ve been working on a game of my own off and on for the last many months (around my client iPhone and Mac projects), and these tutorials came at a perfect time as I was wanting to revamp the font engine and reorganize and refine the graphics engine code a bit. I watched all 11 of these tutorials yesterday and last night and they’re quite valuable. I look forward to seeing your book. You also found some great resources on the web. Kudos on all of that and for being willing to share.

A couple of constructive comments, which I hope you’ll take in that spirit. : )

First, a minor peeve. “angelcode” is pronounced “ane jell code” not “ang gull code.” Angels, with wings and halos, not degrees; that’s why there’s a halo on the site logo. Very minor thing, but since I’ve heard it mentioned in many of the tutorials, I just wanted to point it out.

Secondly, I think it’s a bit unfortunate that you gave up on Interface Builder in the last tutorial. While it’s certainly possible to avoid the use of IB for any iPhone (or Mac) app development, it can be quite helpful.

For example, just considering windows, different kinds of views (OpenGL plus regular views with buttons, etc.) can be swapped in, layered, hidden/shown and so on and IB is a great way to lay these out. In the case of your menu scene and controls, it would save you a lot of work if you’d just used a regular view with custom buttons in it (transparent over OpenGL drawing or with images) and just passed those events to your game controller for starting new games, etc. This would get rid of the need for any of your control classes and you could transparently overlay this on an OpenGL view (or even add the buttons in an OpenGL view itself) if you wanted to do some open GL effects when a button was pressed. The current menu effects you’re using could be done without OpenGL at all, but I can see that you might want to add some OpenGL effects, like your character wandering around the menu buttons. Again, that could be accomplished by layering views. There’s no great reason I can think of to recreate the UIControl and UIButton view classes, which is basically what you’re doing. And again, IB really can be useful for this. Beyond that, if you wanted to pull in any of the other classes from UIWebViews to UITableViews and NSArrayControllers, it’s really helpful to use IB. I’m curious what problems you were running into with it. It’s fine to develop without it, but in most cases I wouldn’t think there would be an advantage to doing that as opposed to setting things up in IB and then tweaking the settings as the nibs are loaded.

I’m also curious if you’ll be adding a true AI engine to the game. There are some great books on this (AI Game Programming Wisdom series, Game Programming Gems series and Programming Game AI by Example to name a few) which have great information about adding things like “flocking” where the bad guys move around together in natural ways. In your ghost example, that small change would be interesting.

Anyway, I look forward to your ongoing work. I enjoyed watching all the videos yesterday; thanks for sharing your experiences.

Dave

mike  on December 22nd, 2009

Hi Dave and thanks for the great feedback. I’m always happy to take advice and learn from it :o)

I’ll certainly take the point about AngelCode and its pronunciation :o) I must admit that I had missed the halo on their site . Looking at their site now its so obvious on the logo…DOH!!!

As for IB I think it was more lack of practice than anything else. I’ve actually come to use it a bit in Sir Lamorak’s Quest. The main menu I have is still all OpenGL, but there is no funky code anymore recreating the UIKit classes, I just look for a button to be touched. I’ve had mixed results mixing UIKit and OpenGL. On the whole its performed ok, but there have been times when it runs like a dog on the iPhone. More so with the 2G/3G.

I took the comments from Apple to heart in their docs and tried to mix UIKit and OpenGL as little as possible. I am however using UIAlertViews over OpenGL and also for my high score view and settings. They are themed to look like the game but are just UIKit controls underneath all done through IB. The high score view is also making use of a tableview etc.

I’ve gotten much more used to IB now and I agree it does save a HUGE amount of time. When I’m done with the book and don’t have tight timescales I’m going to play more with it to see how I can work better with IB and OpenGL.

I’m working on a small tutorial at the moment on saving game state using NSCoding which is how I do things on SLQ. I’m going to include a little sneak peak inside SLQ as well and let people see how its looking. You’ll see the IB stuff in that video.

Thanks again for the feedback. I’ve been consistently impressed by people taking the time to comment, question or help out. It’s great to have such an open community all heading in the same direction.

Cheers

Mike

mcgoogles  on December 22nd, 2009

Mike – I look forward to the next video. I hope you take some time to enjoy the holidays.

9livesoftware – I took a look at your app and got it working. The biggest issue is that you’re doing everything in Landscape orientation and the tutorials haven’t covered the changes necessary to do that (mainly for the coordinate system issues).

I’ve posted a revised version of your project (see below), but here are some thoughts in case anyone else is working on a landscape orientation.

1. To rotate the view when you load it. I used an outlet to your gameView and did this:

    CGAffineTransform flip90Transform =
    CGAffineTransformConcat(CGAffineTransformIdentity,
    CGAffineTransformMakeRotation(M_PI /2));

[super viewDidLoad];

// Rotate the view 90 degrees to keep the landscape mode we used in IB.
[gameView setCenter: CGPointMake(160, 240)];
[gameView setTransform: flip90Transform];

If your app is landscape-only, there’s a flag you can set in your info.plist file for this (UIInterfaceOrientation), however there are still times you need to do this transform.

2. Even though the view will rotate, the X and Y values will not be swapped (X doesn’t suddenly go from 0-480 and y from 0-320. So, you need to deal with this. II changed your viewDidLoad to pull the joypad location from the imageview so that you can reposition it in IB and it will still work:

	CGFloat  aFloat;

	joypad = [joypadImage frame];

	// Fix things up since we rotate 90 degrees.
	aFloat = joypad.origin.y;
	joypad.origin.y = joypad.origin.x;
	joypad.origin.x = 320 -aFloat -joypad.size.height;
	aFloat = joypad.size.width;
	joypad.size.width = joypad.size.height;
	joypad.size.height = aFloat;

	joypadCentery = joypad.origin.x + (320 - joypad.size.width /2);
	joypadCenterx = joypad.origin.y + (joypad.size.height /2);
	joypadMaxRadius = 50;

3. The touch locations should really be relative to the screen, not the view they were tapped in:

// Get the point where the player has touched the screen
        CGPoint touchLocation = [touch locationInView: nil];

4. And finally the biggest changes are in the touches moved:

// Get the point where the player has touched the screen
CGPoint touchLocation = [touch locationInView:nil];

// Calculate the angle of the touch from the center of the joypad
float dx = ((float)joypadCenterx - (float)touchLocation.y);
float dy = (float)joypadCentery + (float)touchLocation.x -320.0;

distance = sqrtf(dx * dx + dy * dy);

...skip...

// ...set the location of the joypad cap to the location of the players touch
CGPoint	newCenter;
CGPoint	newCenter;
newCenter.x = joypadCenterx -dx;
newCenter.y = joypadCentery -dy;
joypadCap.center = newCenter;

I’m trying to keep this message condensed for everyone who’s not interested, but basically you’re dealing with the coordinate system change again.

I also turned off the status bar in your viewController and nib since your game view was 480×320, which means there can’t be a status bar.

One final thing, the _vector-brick-grafitti… image was missing from the sources so I had to disable it in the build. I’m guessing you’re referencing it from another place on your drive.

The updated source is here:

ftp://66.189.87.204
anonymous login, download: Bumper2.zip

I left your original code in, disabled and flagged by “//CHANGE” comments. If you wanted to have the app work in both orientations, you’d obviously want to come up with a more universal approach. These changes just make it work in landscape.

Dave

mike  on December 23rd, 2009

Hi Dave and thanks for helping out 9livessoftware. As I said earlier it’s great to see people helping each other :oD

We did cover how to rotate your drawing in an EAGLView earlier in the tutorials, although it was very brief and did not include anything done inside interface builder.

My only word of caution is not rotate an OpenGL View by transforming its view. Doing that causes the contents of the view to be transformed each frame and has a very large performance impact.

Usually the EAGLView does not have its orientation changed and instead you use OpenGL inside your code to rotate the view as necessary using glRotate. This is shown in the tutorial code although I can’t remember which one :o)

Mike

mcgoogles  on December 23rd, 2009

Ah, good info, Mike. That’s how I sometimes handle orientation issues in non-Open GL iPhone apps (ie. not games), but what you’re saying makes sense. Thanks for the heads up!

Dave

mike  on December 23rd, 2009

Hi RoberRM

If you go to Vimeo and check out the bottom left hand corner of the video screen there is a download option. This is only available for a few days after the video is up, or until we hit out limit then the download goes away.

Hopt that helps.

Mike

dougontour  on December 24th, 2009

Hi Guys,
I have a question that I know is mostly around logic … well at least i think so ..

I’ve been playing around with the joypad example project and while i have things going the way I want them to, although I think I may have fried my brain :)

What I have is a UIImageView for the background and the player, so, when the joypad is moved, I am moving the player, but also, slightly moving the background UIImageView in the apposing direction .. (I am not doing a great job of explaining this already… )

The logic issue I am having is keeping the player ship within a small rectangle within the screen bounds ..

So .. for example ..

If (player.center.y >= 200) {
// dont let him go any further but let him move down ..
}

The issue I am having is that if i implement this type of code, once the playership hits that corordinate, the joypad control of moving him down doesnt work .. so the player is stuck at the top of the section ..

Or…

Am I better off having a collision layer and dealing with it that way?

I am pretty sure I am not wording this as well as I could .. but i think the main point is in there somewhere :)

Regards

D

mike  on December 24th, 2009

Hi Doug. So from what you have said, as long as the joypad is moved you want the background to scroll in the opposite direction, but the ship itself should only move within a small area?

Mike

dougontour  on December 24th, 2009

I could have save a lot of words saying it that way :) but yes :)

Doug

Keith  on December 28th, 2009

Hi Mike,

I have a problem with multi-touch. When I holding down a finger on the joypad and press my fire button at the same time, the touchend event of the fire button finger got the sametouch hash as the joypad touch. I am wondering if the touch hash works as expected?

Thanks for the great tutorials anyways!
Keith

mike  on December 28th, 2009

Hi Keith

That is odd and doesn’t sound right. I’ve not seen that behaviour before and I’ve not seen it in my game. Is this happening with the Joypad example code?

Mike

Anonymous  on December 28th, 2009

Hi Mike,

Thanks for the quick respond. I have figured out the problem. I was getting the touchend from the event->AllTouches instead of touches. Thats why it reseted the moving state of the joypad.

Thanks,
Keith

Keith  on December 30th, 2009

Hi Mike,

Thanks for your reply. I have figured out the problem. I was using AllTouches for looping in the TouchEnd event instead of using touches.

Thanks,
Keith

Incredible iPhone Game Programming Tutorials With Video | iPhone Development Tutorials and Programming Tips  on January 2nd, 2010

[...] iPhone Game Programming – Tutorial 1 iPhone Game Programming – Tutorial 2 – Image Class iPhone Game Programming – Tutorial 3 – SpriteSheet Class iPhone Game Programming – Tutorial 4 – Bitmap Font Class iPhone Game Programming – Tutorial 5 – Animation Class iPhone Game Programming – Tutorial 6 – Tiled Map Class iPhone Game Programming – Tutorial 7 – Singleton Class iPhone Game Programming – Tutorial 8 – Particle Emitter iPhone Game Programming – Tutorial 9 – Sound Manager iPhone Game Programming – Tutorial 10 – Game Structure iPhone Game Programming – Tutorial 11 – Joypad [...]

AnotherMike  on January 27th, 2010

This site is great, it’s the reason I have been able to pick up openGl ES and iPhone programming so fast. Thanks Mike

I was trying to enable multi touch in my app and it would not register any touches after the first touch, it would register multiple touches if they happened at the same time, but not at different times. Apparently you need to enable multi touch in each view you use, so you need to add the folowing to each views init.

[self setMultipleTouchEnabled:YES];

http://developer.apple.com/iph.....uchEnabled

mike  on February 5th, 2010

Hi AnotherMike, thanks for the comments. You are of course absolutely right in that you need to enable multitouch on the views where you need it. The tutorial code didn’t do that as I was only tracking the joypad, but to enable the player to also tap the screen to fire etc you would need multitouch enabled.

Nice spot and thanks for feeding that back into the blog :o)

Mike

KenF  on February 8th, 2010

Mike – this tutorial as usual is excellent. Is there a download link for the video, I’d love to add this to my collection, so I can view it at anytime, even without a WiFi connection.

Thanks.

blakeman13  on February 20th, 2010

Hi Mike
Ive been working on a game based on your tutorials and Ive hit a snag on collision detection. How did you do collision detection amoung bthe entities and the player on SLQ?

Steve  on February 24th, 2010

Hey Mike

Great Tutorials I’ve been following them since the first one, cant wait for the book. I’m trying to do a similar Zelda styled game my first question is:

I’ve been trying for days now and i cant get the joystick to work in the code from tutorial 10 i know it has something to do with defining the the float x and y values in the player.m file but i cant figure out what i should set them to

second i don’t know why this is happening but when i change the .gif files for the player animations the project builds fine on but only runs on the simulator but not the iphone (SIGABRT)

also lastly i am getting 27 warnings the say DEBUG redefined, i think it’s coming from the common.h file

Any help or insight would be greatly appreciated,

THX in advance

mike  on February 24th, 2010

Hi Steve, there are so many things it could be its hard to point to one area. Do you have an example project where you are mixing the two. If so send it over and I’ll take a look.

The DEBUG problem is my fault. Using DEBUG as a constant is not a good idea so they need to be changed to something else like SLQDEBUG.

Mike

Romeorx2  on February 24th, 2010

Hey Mike

Thanks a lot for replying so quickly.

Here’s my project:

http://www.mediafire.com/file/jytcnoyjdgx/Iron Dragon.zip

i haven’t done much to it yet but as soon as i figure out this joystick thing i’ve gotta try and change it to landscape mode, add some support for gestures, and find a way implement some zooming that looks believeble

THX in advance again

Romeorx2  on February 26th, 2010

As far as the zooming I was thinking of using the scale method, what do you think?

Steve  on February 28th, 2010

Hey Mike
If I where trying to implement this joystick into the game from tutorial 10, what x and y values would I read into the player.m file

PLS help I and really stuck here

P.S. Thanks for putting this tutorial together

mike  on February 28th, 2010

Hi Steve

You would not necessarily read in an x and y value from the joypad. Instead you would use the angle between the centre of the joypad and the users touch. The angle can be passed into the player object and used to calculate how the player should move e.g.

player.x = (speed * delta) * cosf(angle);
player.y = (speed * delta) * sinf(angle);

Mike

Steve  on February 28th, 2010

Here is what i am use in the player object but it’s not work my guess is the the the values that are in the up down left and right if statments need to be change but i dont know what to change them to

Pls let me know what I’m doing wrong

playerLocation.x = (aDelta * playerSpeed) * cosf(playerAngle);
playerLocation.y = (aDelta * playerSpeed) * sinf(playerAngle);

// Track if the player is moving
BOOL _moving = NO;

switch (entityState) {
case kEntity_Alive:
// Up
if(playerLocation.y >= -0.72) {
tileY -= _playerSpeed;
float xx = ((tileX * _tileWidth) + 28) / _tileWidth;
float yy = ((tileY * _tileHeight) – 36) / _tileHeight;
if([_scene isBlocked:xx y:yy] ||
[_scene isBlocked:tileX y:yy]) {
tileY += _playerSpeed;
}
_currentAnimation = _upAnimation;
_moving = YES;
}

// Down
if(playerLocation.y <= -0.85) {
tileY += _playerSpeed;
float xx = ((tileX * _tileWidth) + 28) / _tileWidth;
if([_scene isBlocked:xx y:tileY] ||
[_scene isBlocked:tileX y:tileY]) {
tileY -= _playerSpeed;
}
_currentAnimation = _downAnimation;
_moving = YES;
}

// Left
if(playerLocation.x = 0.12) {
tileX += _playerSpeed;
float xx = ((tileX * _tileWidth) + 28) / _tileWidth;
float yy = ((tileY * _tileHeight) – 36) / _tileHeight;
if([_scene isBlocked:xx y:yy] ||
[_scene isBlocked:xx y:tileY]) {
tileX -= _playerSpeed;
}
_currentAnimation = _rightAnimation;
_moving = YES;
}

mike  on February 28th, 2010

What is happening at the moment when you run your code and what do you want to happen :o)

Mike

Steve  on February 28th, 2010

Right the the player just walks straight up I want the joyStick to move him around like in Sir Lamerik’s Quest :-)

Steve  on March 1st, 2010

Should i even be pulling data from the game scene or should all the code for the be in the player object?

mike  on March 1st, 2010

Well the game scene is being passed the touch information and the joypad is implemented in the game scene. This I would say is the best place to create the information and then pass what is necessary to the player object i.e. the angle of the joypad touch and speed.

Is the player walking in a straight line without you touching the joypad? If that is the case then maybe the accelerometer code is still switched on.

Sorry I’ve not been able to look at this much. The edits for the book are starting to come in so I’m busy working on those.

Mike

Steve  on March 1st, 2010

I was thinking the same thing but whenever I to pass a variable like playerLocation.x & y, dx & dy, or player angle. The build fails if I define them as floats and if I define them as ints the project builds with warning but the player won’t move at all… I’m lost

(I did comment out the accelerometer code so I know it’s not that)

Can’t wait for the book, and tutorial 10 part 2 lol

Steve  on March 1st, 2010

this is what i am using to pass the data to the player object

[/code]
float angle = [(GameScene*)[_sharedDirector currentScene] playerAngle];

i used the NSLog to check the values that playerAngle returns and the number ranges from -3.000000 to 3.000000

this is what i am doing with the data

// Up
if(angle <= -0.000001) {
tileY -= _playerSpeed;
float xx = ((tileX * _tileWidth) + 28) / _tileWidth;
float yy = ((tileY * _tileHeight) - 36) / _tileHeight;
if([_scene isBlocked:xx y:yy] ||
[_scene isBlocked:tileX y:yy]) {
tileY += _playerSpeed;
}
_currentAnimation = _upAnimation;
_moving = YES;
}
[code]

basically i changed the if statements to reflect the data from playerAngle instead of the data from the accelerometer

I would think this would be the correct way of doing this but it is working for me

Iphone Cocos2d Day 2 – Scrolling TileMap & JoyPad « Learn Cocos2d & Box2d for Iphone  on March 2nd, 2010

[...] or right, but I worked beautifuly. I got the concept for the code from a great tutorial website 71squared. The tutorial 11 on that website talks about creating a joypad. I had to modify it a fair bit to [...]

Steve  on March 4th, 2010

hey Mike just wondering if you had any ideas on what i’m doing wrong

Steve  on March 4th, 2010

Hey I’ve made a little progress but I’m not there yet here is the current project

http://www.mediafire.com/?y2mzmqkmvzz

mike  on March 4th, 2010

Thanks Steve. Sorry I’ve not been able to look at this yet, been really busy with worth and editing. I’ll try and take a look ASAP.

Mike

Steve  on March 6th, 2010

hey mike

Tommy  on April 15th, 2010

Hey Mike
I have to say that this is by far the greatest blog out there for making an Iphone game!!! I’m a total beginner to Iphone programming, Objective C and OpenGl. And your blog has really helped me to make some sense of all these great and difficult things :)) I’ve been spending many mornings avidly watching all your videos besides studying for university.

Still I get really frustrated when I’m trying to do things on my own i.e. I was trying to insert the joypad example code to tutorial 9 and it just doesn’t work. There are no error messages but the joypad doesn’t show up on the screen ;-( I just don’t know how to link one thing to another. I think that you have to modify some code but I just don’t know what or where :D

Any help? Thanks!!!

Tommy

P.S. I can’t wait for your book ;)

mike  on May 16th, 2010

Hey Mike. I just wanted to say first that I love your website and what you do. This is by far the best site I have found for learning iPhone Game programming. I downloaded and commented SLQ, the final loks awesome. I really like the main menu screen. I can’t wait for your book.

I was hoping I could ask a question. Instead of changing the ships position, I want to change the ships rotation based on the angle in the joypad. I have tried using:

[player setRotation: touchAngle]

in the updateWithTouchLocationMoved method. I really though this would work. The ship does rotate a little bit left and right but only a couple degrees. Do you have any idea what I’m doing wrong?

Again. Thank you so much for all your time and effort.

Washington  on May 25th, 2010

Mike Thank you for these wonderfull tutorials, these are priceless my friend!
Here I have the Stanford University video courses but they don´t teach how to create a complete iphone game but you teach and with a good pratices in development.Tell me something…for the ipad is the same thing right? I just have to develop my game with the device dimensions.Also as I am still watching your videos ( I have discovered you yesterday ^_^ ) Do you comment on orientation rotation? like landscape right to landscape left or portrait right to portrait left? Because apple obligates us to do that. But I really don´t thing it´s necessary, but apple does. what do you think about making a tutorial to teach us about using xcode to compile our games to test or for distribution working with the provisioning keys( I know every app must have it´s own ID , it´s own provision key and our general certification but how to configure and generate for example an app to sell?

I wanna make an iPhone game…now what? « iPhone Friend  on July 22nd, 2010

[...] ParticleDesigner. Excellent, clear tutorials. The joystick for my current game project was based on this tutorial and some other theory and code of my own [...]

Nandini  on July 28th, 2010

Hi Mike,

How can i use joystick to allow only 4 direction? up,dowm,left and right?

Thanks,

mike  on July 28th, 2010

@mike, Washington, sorry for the huge delay in replying to your posts. I missed the emails telling me that comments had been made to this post.

mike, As long as the touch angle is in degrees and not radians, it should work OK. If you still have a problem or any code you can share I’ll take a look.

@Washington, I did cover orientation in the book and how I have done it for SLQ. That should give you what you need to get basic orientations supported. I may look at doing more on this in tutorials once the book is out.

@Nandini, with the joypad code, you could check to see what the players touch is on the joypad from the centre or the pad, breaking that into quandrants i.e. up, down, left and right means that with the touch in a defined quadrant, the players ship can be made to move up, down, left and right. I hope that makes some sense :o)

Mike

Geir  on August 8th, 2010

I’m going nuts here.. I get:

Unable to synchronize the audio input and output devices’ sample rates
AddRunningClient starting device on non-zero client count

with every app that tries to use AVAudioPlayer on the iPhone OS4.0 simulator (works perfect on the device). This app, my own app, even Apple’s avTouch test app.
I’ve been struggling all day trying to find out why, but nothing.. :(
I’m about to revert back to XCode 3.2.2 if I can’t fix this soon. Anyone have any slightest idea ? I’ve even tried it on two different machines.

mike  on August 10th, 2010

Hi Geir

I get the same message as well, although for me sound still plays on the simulator. It also works fine on the device as well.

I’ve been looking for a solution and not found anything that has sorted it out. If I come across anything I’ll post it on the blog.

Sorry I can’t be of more help.

Mike

Geir  on August 10th, 2010

Thanks for the reply :)

It won’t play audio on the simulator on my Macbook, but it plays on the Mac Pro at my office. Still the same weird message though. Both run the same XCode & SDK.
At least it works fine on the device itself.
I have found lots of questions about it, but no answers. I have filed a bug report about it so I’ll wait and see what happens. If I hear anything I’ll post it here.

mike  on August 10th, 2010

That would be great, thanks Geir.

Mike

the start of a new journey  on August 14th, 2010

[...] am removing all the game logic of Panic Block from the Game Engine. I managed to integrated the Joypad Control and hopefully to complete theTile-Map class [...]

I wanna make an iPhone game…now what? | WasabiSoft  on September 1st, 2010

[...] ParticleDesigner. Excellent, clear tutorials. The joystick for my current game project was based on this tutorial and some other theory and code of my own [...]

iPhone Game Programming Tutorial 1 | Hardblog Cafe  on October 6th, 2010

[...] iphone-game-programming-tutorial-9-sound-manager iphone-game-programming-tutorial-10-game-structure iphone-game-programming-tutorial-11-joypad iphone-game-programming-tutorial-12-saving-state tutorial-13-app-store-submission AKPC_IDS += [...]

mamat  on October 31st, 2010

cane have a look with my code.
http://www.4shared.com/file/qpZUVvXv/myGame.html
seems to be the output is different from the simulator and on my device. and how possible to make logic of the game.thanks

Geoff  on November 2nd, 2010

An amazing bit of work you did here on this tutorial set!! I started learning C over 20 years, but gave up I learned Visual Basic instead and now I feel overwhelmed! :( Oh well. At the moment, however, these tutorials won’t run on my Macbook Pro because I don’t have xcode 2.2 and Apple doesn’t appear to want developers to have or use those older SDKs. Is there a way to upgrade these tutorials to run in SDK 3 or greater?? Thanks if you can help1 :)

mike  on November 3rd, 2010

Hi Geoff

If you edit the project settings and change the base SDK to your current SDK, the projects should all work fine.

Let me know how you get on.

Mike

memphis  on December 13th, 2010

Hello Mike,
I just updated xcode to latest version and I have a little problem. The problem is that my touches (Began, Moved and ended) are not being called at all. I’m using your suggestion about not using NSTimer and create your own gameloop. and I did and in previous version of SDK it worked perfectly. but in new update nothing works. I created a sample OpenGL and and touches are called but once I use one of the the methods below the touch won’t be triggered.

#option one:

[self performSelectorOnMainThread:@selector(gameLoop) withObject:nil waitUntilDone:NO];

#option two:
[NSThread detachNewThreadSelector:@selector(gameLoop)
toTarget:self withObject:nil];

I have to remind you that inside my game loop I have the following loop as well to catch all my touches:

while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.002f, TRUE) == kCFRunLoopRunHandledSource);

another odd thing is that the touches will be called if I compile my program agains IPad Simulator.

I’m kind of lost, search all over net for 2 days and really need your help.

Thanks
Ali

mike  on December 13th, 2010

Hi Ali

Can you send me an example project to mike@71squared.com and I’ll take a look. Not sure off the top of my head what the problem could be without looking.

Mike

theclayton  on February 1st, 2011

Hey, I have a question. I’ve been trying to develop a game that your character or “ship” stays in the middle of the screen and the background moves behind it. Do you know how I would do this using the joy pad?

Thanks.

theclayton  on February 1st, 2011

Hey, I have a question. I’ve been trying to develop a game that your character or “ship” stays in the middle of the screen and the background moves behind it. Do you know how I would do this using the joy pad?

Thanks,

clayton

AmitSri  on March 23rd, 2011

Hi,

I am following your Tutorial 11 and try to run the project in iphone simulator 4.2 and getting following error.

AudioStreamBasicDescription: 2 ch, 44100 Hz, ‘lpcm’ (0x00000C2C) 8.24-bit little-endian signed integer, deinterleaved

After getting this error in simulator i can see only white screen.

Hope you will find some solution soon.

Thanks

Kyle  on April 27th, 2011

You are my hero !!!! Thank you so much to sharing these knowledge!!!!

Leave a Comment