Quote:
Originally Posted by noaim
Hi Guys,
i have a problem with a code,
i want that the variable live is set into -1*if CGRect the ball frame and the snake frame.
My Code:
if (CGRectIntersectsRect(ball.frame, snake.frame)) {
if (ball.center.y + 10 > snake.center.y) {
if (ballVelocity.y < 0) {
snakeVeloctiy.y = .1;
live = live -1;
}
}
}
But then i lost live a few times for each collision in this few sec. i only want that itīs loose it for one time.
I hope you understand my Problem
So Thanks for every Help!
Greetings Andreas
|
If I understand your question, then one way you could fix it is to create a new integer, set it equal to 0, then add 1 to that instead of live. Then ask if the new integer is equal to 1, and if it is subtract 1 from live. Then when the rectangles stop intersecting, set the new integer back to 0.
It would look something like this:
Code:
//Before your main loop: Integer myInteger = 0;
if (CGRectIntersectsRect(ball.frame, snake.frame)) {
if (ball.center.y + 10 > snake.center.y) {
if (ballVelocity.y < 0) {
snakeVeloctiy.y = .1;
myInteger++;
if (myInteger == 1) {
live--; // live-- is the same as live = live - 1
}
}
}
}
else {
myInteger = 0;
}
I don't know if this is the best solution, but I think it will work.
P.S. It's not a good idea to expect an answer in the first few hours on any forum.
http://www.iphonedevsdk.com/forum/ip...uidelines.html