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 Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-10-2011, 12:33 AM   #1 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Question moving Image gets stuck off screen

Hi guys,

I followed a tutorial to make a 'dodge' style game. I've adapted mine so that when the moving images are bumped by another image, they animate and move in another direction.

My problem is that if I bump the image beyond the boundaries they get get 'stuck' of screen...

Any advice on how I can fix this problem?

Thanks

Matt

Here is the part of my code that 'should' keep the images within the screen... what am i doing wrong?

Code:
- (void) onTimer {
    
    q1.center = CGPointMake(q1.center.x+pos4.x, q1.center.y+pos4.y);
    
    if (q1.center.x > 320 || q1.center.x < 0)
        pos4.x = - pos4.x;
    
    if (q1.center.y > 480 || q1.center.x < 0)
        pos4.y = -pos4.y;
    
    q2.center = CGPointMake(q2.center.x+pos5.x, q2.center.y+pos5.y);
    
    if (q2.center.x > 320 || q1.center.x < 0)
        pos5.x = - pos5.x;
    
    if (q2.center.y > 480 || q2.center.x < 0)
        pos5.y = -pos5.y;
My pos4 and pos5 are defined as following in ViewDidLoad:

Code:
pos4 = CGPointMake(7.0,6.9);
    
pos5 = CGPointMake(7.0,2.6);
marshusensei is offline   Reply With Quote
Old 05-10-2011, 01:40 AM   #2 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 346
mashercakes is on a distinguished road
Default

I think your lines "pos4.x = - pos4.x" should be:

Code:
pos4 = CGPointMake(-pos4.x, pos4.y);
And then obviously the same for all the other ones.
__________________
PicBoard - a visual support app for children with autism, communication difficulties or learning difficulties. Available now for iPad.

TalkBoard - Adds Communication Aid features to PicBoard, for non-verbal children or adults. Available now for iPad.
mashercakes is offline   Reply With Quote
Old 05-10-2011, 02:32 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 99
Mizonnz is on a distinguished road
Default

Code:
   if (q1.center.y > 480 || q1.center.x < 0)
should be

Code:
   if (q1.center.y > 480 || q1.center.y < 0)
Same for q2 further down.
Mizonnz is offline   Reply With Quote
Old 05-10-2011, 06:48 AM   #4 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Default Thanks for you help

Thanks for your help guys, tried those things, but it still gets stuck off screen... Will keep trying things...
marshusensei is offline   Reply With Quote
Old 05-10-2011, 07:10 AM   #5 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Default Animation is the cause

I think it is my rotating animation command that gets the images stuck off screen (once they've passed the boundaries) the rotate animation seems to override the boundary??? Any advice on this

Here is my animation code:

Code:
       //animation - spin the thing if hit
        
        [UIButton beginAnimations:nil context:NULL];
        [UIButton setAnimationDuration:1.5];
        
        q1.transform = CGAffineTransformMakeRotation(3.054326191);
        
        q1.transform = CGAffineTransformIdentity;
        
        [UIButton commitAnimations];
        
        //end animation
marshusensei is offline   Reply With Quote
Old 05-10-2011, 09:10 AM   #6 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 118
Feldspar is on a distinguished road
Default

if something else is updating the .center positions, you could end up getting stuck. for example, if the x position is too far to one side, then reversing direction might not be enough to put it back inside the boundary. in this case, the boundary check would continue to fail every frame, causing it to reverse direction back and forth constantly.

try something like this:
Code:
if (q1.center.x > 320) {
     q1.center.x = 320;
     pos4.x = -pos4.x;
}
if (q1.center.x < 0) {
     q1.center.x = 0;
     pos4.x = -pos4.x;
}
// etc for q1.center.y, q2...
Feldspar is offline   Reply With Quote
Old 05-11-2011, 11:10 PM   #7 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Default Feldspar

Hey mate, tried this but had error with

Code:
q1.center.x = 0;
expression not assignable,

will keep trying. thanks for your help
marshusensei is offline   Reply With Quote
Old 05-11-2011, 11:18 PM   #8 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 154
Bobarino is on a distinguished road
Default

Quote:
Originally Posted by marshusensei View Post
Hey mate, tried this but had error with

Code:
q1.center.x = 0;
expression not assignable,

will keep trying. thanks for your help
Hello marshusensei,
The links below are for a dodge game tutorial that works, hope this helps:
Part 1

YouTube - iPhone SDK Tutorial: Making An Image Dodging Game (Part 1)

Part 2

YouTube - iPhone SDK Tutorial: Making An Image Dodging Game (Part 2)

Part 3

YouTube - iPhone SDK Tutorial: Making An Image Dodging Game (Part 3)
Bobarino is offline   Reply With Quote
Old 05-11-2011, 11:45 PM   #9 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 346
mashercakes is on a distinguished road
Default

Quote:
Originally Posted by marshusensei View Post
Hey mate, tried this but had error with

Code:
q1.center.x = 0;
expression not assignable,

will keep trying. thanks for your help
It should be:

Code:
[q1 setCenter:CGPointMake(0, q1.center.y)];
__________________
PicBoard - a visual support app for children with autism, communication difficulties or learning difficulties. Available now for iPad.

TalkBoard - Adds Communication Aid features to PicBoard, for non-verbal children or adults. Available now for iPad.
mashercakes is offline   Reply With Quote
Reply

Bookmarks

Tags
animation, boundary, cgpointmake, stuck

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: 346
8 members and 338 guests
iOS.Lover, lorrettaui53, MikaelBartlett, oztemel, pbart, PlutoPrime, thephotographer, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

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