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 10-27-2011, 08:07 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 14
Danny159 is on a distinguished road
Default New Developer - Simple Game Help

Hey All,

I am new to iOS development I am a PHP developer.

I have started a really simple game... get the ball from the start point to the hole...

All working fine so far the ball rolls round the screen, doesn't go off the screen and when you get it in the hole an alert comes up and you can restart....

Anyway now I have that working I have decided to be more adventurous and add obstacles (UIImageViews) walls...

However I am really stuck... I have been stuck on this for weeks and google doesn't seem to help =(

The ball is rolling around the screen using the accelerometer... I added the wall into the .xib file and assigned the *wall I defined in the .h file...

But I can for the life of me stop the ball from rolling though the wall... I can detect a collision using this code:

Code:
if(CGRectIntersectsRect(ball.frame, wall1.frame)){
        NSLog(@"Wall was hit");
}
Can anyone please help me in stopping the call from going through the wall, the users has to navigate round the wall.

Dan
Danny159 is offline   Reply With Quote
Old 10-27-2011, 08:40 AM   #2 (permalink)
Registered Member
 
SundialSoft's Avatar
 
Join Date: Oct 2010
Location: Scotland
Posts: 176
SundialSoft is on a distinguished road
Default

Quote:
Originally Posted by Danny159 View Post
Hey All,

I am new to iOS development I am a PHP developer.

I have started a really simple game... get the ball from the start point to the hole...

All working fine so far the ball rolls round the screen, doesn't go off the screen and when you get it in the hole an alert comes up and you can restart....

Anyway now I have that working I have decided to be more adventurous and add obstacles (UIImageViews) walls...

However I am really stuck... I have been stuck on this for weeks and google doesn't seem to help =(

The ball is rolling around the screen using the accelerometer... I added the wall into the .xib file and assigned the *wall I defined in the .h file...

But I can for the life of me stop the ball from rolling though the wall... I can detect a collision using this code:

Code:
if(CGRectIntersectsRect(ball.frame, wall1.frame)){
        NSLog(@"Wall was hit");
}
Can anyone please help me in stopping the call from going through the wall, the users has to navigate round the wall.

Dan
Hi Danny, your approach is fine for very simple stuff but won't lead to a real app. I suggest you start by looking at these tutorials:- How To Make A Simple iPhone Game with Cocos2D Tutorial | Ray Wenderlich

To make a decent 2D or 3D game you need to get away from using the high level objects like uiimageviews etc which are made for apps not games. Ray uses cocos2d in his tutorials. Cocos2d also has a 3d extension in beta (cocos3d).

You can get your current code to work. You don't say how the ball is moving. Is it a uiimage being moved by coordinate changes coming from accelerometer data? You need to stop the movement when there is a collision and deal with restarting when the direction changes.
SundialSoft is offline   Reply With Quote
Old 10-27-2011, 09:11 AM   #3 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 14
Danny159 is on a distinguished road
Default

Quote:
Originally Posted by SundialSoft View Post
Hi Danny, your approach is fine for very simple stuff but won't lead to a real app. I suggest you start by looking at these tutorials:- How To Make A Simple iPhone Game with Cocos2D Tutorial | Ray Wenderlich

To make a decent 2D or 3D game you need to get away from using the high level objects like uiimageviews etc which are made for apps not games. Ray uses cocos2d in his tutorials. Cocos2d also has a 3d extension in beta (cocos3d).

You can get your current code to work. You don't say how the ball is moving. Is it a uiimage being moved by coordinate changes coming from accelerometer data? You need to stop the movement when there is a collision and deal with restarting when the direction changes.
Hi Thanks for your post back.
I will have a look at that website.

Getting this to work would be good just go I can see how the code would work.

Yes that is correct... I have detected the ball hitting the wall but im not sure on how to deal with it now... here is some of my code:

Code:
// Start Exceloromiter
    CGRect frame = [ball frame];
    frame.origin.x = 145.0f; // was 146.0f
    frame.origin.y = 343.0; // was 372.0
    [ball setFrame:frame];
    
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1/60.0)];
    [[UIAccelerometer sharedAccelerometer] setDelegate:self];

Code:
-(void)moveBoxWithX:(float)xAmount andY:(float)yAmount {
    CGPoint boxCenter = ball.center;
    
    [self CheckForObstruction];
    [self DetectHoleHit];
    
    boxCenter.x += xAmount;
    boxCenter.y += yAmount;
    
    NSString *deviceType = [[UIDevice currentDevice] model];
    
    if([deviceType isEqualToString:@"iPhone"]){
        if(boxCenter.x < 36.0)
            boxCenter.x = 36.0;
        if(boxCenter.x > 283.0)
            boxCenter.x = 283.0;
        
        if(boxCenter.y < 36.0)
            boxCenter.y = 36.0;
        if(boxCenter.y > 423.0)
            boxCenter.y = 423.0;
    }
    
    else if([deviceType isEqualToString:@"iPod touch"]){
        if(boxCenter.x < 19.0)
            boxCenter.x = 19.0;
        if(boxCenter.x > 299.0)
            boxCenter.x = 299.0;
        
        if(boxCenter.y < 19.0)
            boxCenter.y = 19.0;
        if(boxCenter.y > 439.0)
            boxCenter.y = 439.0;
    }
    
    ball.center = boxCenter;
    
}

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
	float sensitivity = 25.0f;
	float xDistance = acceleration.x *sensitivity;
	float yDistance = acceleration.y *-sensitivity;
	
	[self moveBoxWithX:xDistance andY:yDistance];
	
}
Danny159 is offline   Reply With Quote
Old 10-27-2011, 12:44 PM   #4 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 14
Danny159 is on a distinguished road
Default

Hey Again guys...

I have got this far...

Code:
if(CGRectIntersectsRect(ball.frame, wall1.frame)){
        NSLog(@"Hit A Wall");
        [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1/0.0)];
        [[UIAccelerometer sharedAccelerometer] setDelegate:self];
}
So when my ball rolls into the wall it stops the ball from rolling but I'm stuck... HELP PLEASEEE!!
Danny159 is offline   Reply With Quote
Old 10-31-2011, 09:35 AM   #5 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 14
Danny159 is on a distinguished road
Default

Still need help with this!
Dan
Danny159 is offline   Reply With Quote
Old 11-13-2011, 01:03 AM   #6 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 100
XcodeMagic is on a distinguished road
Default

Quote:
Originally Posted by Danny159 View Post
Still need help with this!
Dan
You have a very simple problem.. I can point you to a great tutorial that will teach you about a ball intersecting a pad and the ball does not move through the pad like this : Making a simple iPhone game, part 1 - YouTube

Its a 10 part tutorial and it tought me how to code. trust me I would pay 300$ just to watch this if I was to forget everything about coding to learn again. its that good.

But I can also give you a simple code that you will probably want more than just a tutorial.. So here it is: Check your inbox.
XcodeMagic is offline   Reply With Quote
Old 11-17-2011, 06:12 PM   #7 (permalink)
Registered Member
 
Naughty_Ottsel's Avatar
 
Join Date: Aug 2008
Location: Gillingham, Dorset, UK
Age: 19
Posts: 218
Naughty_Ottsel is on a distinguished road
Send a message via MSN to Naughty_Ottsel
Default

Quote:
Originally Posted by Danny159 View Post
Hey Again guys...

I have got this far...

Code:
if(CGRectIntersectsRect(ball.frame, wall1.frame)){
        NSLog(@"Hit A Wall");
        [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1/0.0)];
        [[UIAccelerometer sharedAccelerometer] setDelegate:self];
}
So when my ball rolls into the wall it stops the ball from rolling but I'm stuck... HELP PLEASEEE!!
Code:
if(CGRectIntersectsRect(ball.frame, wall1.frame)){
        NSLog(@"Hit A Wall");
//check where the ball is hitting
if((wall1.center.x - ball.frame.origin.x) <= (wall1.frame.size.width /2))
{
  //If it was the x axis
   [UIView animateWithDuration:1.0 animations:^(void)animations
 {
   ball.frame.origin.x = ball.frame.origin.x + 5;
 }];

}

else if ((wall1.center.y - ball.frame.origin.y) <= (wall1.frame.size.height /2))
{
  //Otherwise if it is the y axis
  [UIView animatieWithDuration:1.0 animations:^(void)animations
 {
  ball.frame.origin.y = ball.frame.origin.y + 5;
 }];

}

}
Not sure if that will run properly, but hopefully a nudge in the right direction
__________________
Follow me on Twitter
Naughty_Ottsel is offline   Reply With Quote
Old 04-19-2012, 05:53 PM   #8 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 14
Danny159 is on a distinguished road
Default

Quote:
Originally Posted by Naughty_Ottsel View Post
Code:
if(CGRectIntersectsRect(ball.frame, wall1.frame)){
        NSLog(@"Hit A Wall");
//check where the ball is hitting
if((wall1.center.x - ball.frame.origin.x) <= (wall1.frame.size.width /2))
{
  //If it was the x axis
   [UIView animateWithDuration:1.0 animations:^(void)animations
 {
   ball.frame.origin.x = ball.frame.origin.x + 5;
 }];

}

else if ((wall1.center.y - ball.frame.origin.y) <= (wall1.frame.size.height /2))
{
  //Otherwise if it is the y axis
  [UIView animatieWithDuration:1.0 animations:^(void)animations
 {
  ball.frame.origin.y = ball.frame.origin.y + 5;
 }];

}

}
Not sure if that will run properly, but hopefully a nudge in the right direction

Thanks for your reply!!
Sorry its so late...

I added your code but it say there is something wrong with this time... on both of the functions

Code:
[UIView animatieWithDuration:1.0 animations:^(void)animations
             {
                 ball.frame.origin.y = ball.frame.origin.y + 5;
             }];
But im not sure quite what?
Anyone have any ideas?

Dan
Danny159 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: 374
10 members and 364 guests
apatsufas, comicool, Creativ, Dalia, dansparrow, LunarMoon, mer10, Murphy, pbart, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

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