Topic:   My iPhone Game Tutorials for Beginners   (Read 355286 times)


0 Members and 1 Guest are viewing this topic.

Gnome


  • GMG Extraordinaire

  • ***


  • Posts: 1073
Re: My iPhone Game Tutorials for Beginners
« Reply #75 on: June 14, 2010, 08:50:26 PM »
Quote
I am trying to write a platform game for the iPhone. I wrote a collision testing function that looks like this:

-(BOOL) collision: (CGRect)Rect1 Rect2:(CGRect)Rect2 {
if(Rect1.x > (Rect2.x+Rect2.width) || (Rect1.x+Rect1.width) < Rect2.x){
return FALSE;
}
if(Rect1.x > (Rect2.y+Rect2.height) || (Rect1.y+Rect1.height) < Rect2.y){
return FALSE;
}
return TRUE;
}

It almost works. When I try to build, I get these errors:

'CGRect' has no member named 'x'

'CGRect' has no member named 'y'

What are the coordinate values called? Please help.


Sorry mate, Gandolf is out for a 2 weeks.

Wish I could help.
This Cannot be, NOOOOOOOO!!!!

-Gnomes Cry when the McRib was discontinued again.

I-NEED-HELP


  • GMG Newbie

  • *


  • Posts: 40

  • I love YaBB 1G - SP1!
Re: My iPhone Game Tutorials for Beginners
« Reply #76 on: June 15, 2010, 05:38:02 PM »
Instead of using CGRect, I made it work by passing x, y, width and height as parameters.
I've got collision testing working, I am trying something where the platform on the screen is 'solid', so it stops the player when the player tries to move through it. Here is my code:

if(y+height >= platy) {yVel = -1;}
if(y <= platy+platheight) {yVel = 1;}
if(x+width >= platx) {xVel = -1;}
if(x <= platx+platwidth) {xVel = 1;}

But it doesn't work! On two sides, the rectangle is 'solid'. On the other two, the player moves right through to the other side. Please help!

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #77 on: June 18, 2010, 04:20:58 PM »
Hmmmm. Could you post a video or source? I'd like to see what happens.

Also for a CGRect you have to use rect.size.x.


-Gan
P.S. I'm back from my incredibly long vacation without technology.
« Last Edit: June 18, 2010, 04:21:26 PM by Gandolf »

I-NEED-HELP


  • GMG Newbie

  • *


  • Posts: 40

  • I love YaBB 1G - SP1!
Re: My iPhone Game Tutorials for Beginners
« Reply #78 on: June 18, 2010, 07:33:37 PM »
I got collision testing working with one rectangle, I want to be able to loop through an array of say, 50 rectangles. I need an array of CGRects, so I need to know what are the CGRect variables (if public) or methods to access the x position, y position, width and height.

I fixed my code, here is the handleGameTimer function:

- (void) handleGameTimer: (NSTimer *) gameTimer {
//All game logic goes here, this is updated 60 times a second

if (touchlocation.x > screenDimensions.x/2) {
inputxVel = 1;
}
if (touchlocation.x < screenDimensions.x/2) {
inputxVel = -1;
}
if (touchlocation.y > screenDimensions.y/2) {
inputyVel = 1;
}
if (touchlocation.y < screenDimensions.y/2) {
inputyVel = -1;
}

xVel = inputxVel;
yVel = inputyVel;

if([self collision:x y1:y w1:width h1:height x2:platx y2:platy w2:platwidth h2: platheight]){

if(y+height >= platy && y < platy+platheight && yVel == 1) {
yVel *= -1;
}
if(y <= platy+platheight && y > platy&& yVel == -1) {
yVel *= -1;
}
if(x+width >= platx && x < platx+platwidth && xVel == 1) {
xVel *= -1;
}
if(x <= platx+platwidth && x > platx && xVel == -1) {
xVel *= -1;
}
}

x += xVel;
y += yVel;
inputxVel = 0; xVel = 0;
inputyVel = 0; yVel = 0;

//This updates the screen
[self setNeedsDisplay];
}

Please help! Thanks in advance.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #79 on: June 18, 2010, 08:10:38 PM »
You can either (a) Create an array of them by CGRect rectangles[50]; or (b) Create an NSMutableArray to hold the values though the CGRect is not an NS variable and cannot be held in an NSMutableArray.

Once you've made the array just make a for loop that goes through each.


-Gan

I-NEED-HELP


  • GMG Newbie

  • *


  • Posts: 40

  • I love YaBB 1G - SP1!
Re: My iPhone Game Tutorials for Beginners
« Reply #80 on: June 24, 2010, 11:08:08 AM »
Thanks! I got it working.

For your next tutorial, could you show how to make the iPhone interface type buttons (with Interface Builder) and how to load and play sounds?

Thanks for all the help!

I-NEED-HELP


  • GMG Newbie

  • *


  • Posts: 40

  • I love YaBB 1G - SP1!
Re: My iPhone Game Tutorials for Beginners
« Reply #81 on: June 24, 2010, 11:10:12 AM »
Oh, and also I looked up the CGRect variable names: there is a CGPoint called origin and a CGPoint called size. You can access them directly like this:

CGRect.origin.x;
CGRect.origin.y;
CGRect.size.width;
CGRect.size.height;

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #82 on: June 24, 2010, 11:22:00 AM »
Sound and adding buttons are easy. I'll see if I can make a tutorial later though I'm incredibly busy these days.


-Gan

I-NEED-HELP


  • GMG Newbie

  • *


  • Posts: 40

  • I love YaBB 1G - SP1!
Re: My iPhone Game Tutorials for Beginners
« Reply #83 on: June 24, 2010, 03:05:41 PM »
OK. I was wondering - since I am not a paid developer, if you have some sound-playing code and you ran it in the simulator, would your computer's speakers play the sound?

Maybe you should also make a tutorial on how to download the app to an iPhone/iPod Touch for paid developers.

Thanks for all the help!

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #84 on: June 24, 2010, 03:11:26 PM »
Quote
OK. I was wondering - since I am not a paid developer, if you have some sound-playing code and you ran it in the simulator, would your computer's speakers play the sound?
Yeah.

Quote
Maybe you should also make a tutorial on how to download the app to an iPhone/iPod Touch for paid developers.
With Xcode 4 that's suppose to come out soon it'll be incredibly easy. Literally just plug it in and insert your account details.


-Gan

I-NEED-HELP


  • GMG Newbie

  • *


  • Posts: 40

  • I love YaBB 1G - SP1!
Re: My iPhone Game Tutorials for Beginners
« Reply #85 on: June 28, 2010, 12:08:22 PM »
I was trying to have my app display the value of a variable in the upper-left hand corner of the screen, using your [self drawString] function.

I put this code in the drawRect function:

[self drawString:context translate:CGPointMake(0, 0) text:(NSString*)loopCounter point:CGPointMake(0, 0) rotation:0 font:@"Helvetica" color:color size:12];

However, now the app crashes as soon as it starts and never displays the variable. loopCounter is a global variable, I made it in MyCustomView.h. Can someone please help me with drawString?

EDIT:

loopCounter is an int.

Thanks in advance!
« Last Edit: June 28, 2010, 12:10:26 PM by I-NEED-HELP »

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #86 on: June 28, 2010, 12:18:53 PM »
Hmm. Try this:
Code: [Select]
NSString* loopCounterTxt = [NSString stringWithFormat:@"%d", loopCounter];
[self drawString:context translate:CGPointMake(0, 0) text:loopCounterTxt point:CGPointMake(0, 0) rotation:0.0 font:@"Helvetica" color:color size:12];
That should do the trick. Another way to turn an int to a string may be: [loopCounter stringValue]; .


-Gan

I-NEED-HELP


  • GMG Newbie

  • *


  • Posts: 40

  • I love YaBB 1G - SP1!
Re: My iPhone Game Tutorials for Beginners
« Reply #87 on: June 28, 2010, 01:56:24 PM »
Worked perfectly. Thank you so much!

I-NEED-HELP


  • GMG Newbie

  • *


  • Posts: 40

  • I love YaBB 1G - SP1!
Re: My iPhone Game Tutorials for Beginners
« Reply #88 on: June 28, 2010, 02:16:51 PM »
Could you show how to read and write files in the iPhone filesystem? I think that is what some of your functions are for. I want to save a highscore file, but I have no idea how. I will have just one number in the file, which is the highscore. In my program, if the score is greater than the highscore in the file, then the score will replace the highscore. How do I do this? Could you make a tutorial on saving highscores?

Thanks in advance.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #89 on: June 28, 2010, 02:45:54 PM »
Make an NSMutableArray.
Code: [Select]
NSMutableArray* highscore = [NSMutableArray new];
Add your score to the NSMutableArray.
Code: [Select]
[highscore addObject:[NSNumber numberWithInt: 253]];
Then run the save command:
Code: [Select]
[self saveFileInDocs:"Highscore.txt" object:highscore];

That'll save the score 253 in the file "Highscore.txt";

Now to open:
Code: [Select]
NSMutableArray* highscore = [self openFileInDocs:"Highscore.txt"];
int scoreNumber = [[highscore objectAtIndex:0] intValue];

That's how you retrieve and save data.


-Gan