ios - Keep moving images bouncing within frame width and height -
i trying figure out how keep image within frame width , height. right wraps around. preferably create stays within frame , bounces around inside.
-(void) movebutterfly { bfly.center = cgpointmake(bfly.center.x + bfly_vx, bfly.center.y + bfly_vy); if(bfly.center.x > framewidth) { bfly.center = cgpointmake(0, bfly.center.y + bfly_vy); } else if (bfly.center.x < 0) { bfly.center = cgpointmake(framewidth, bfly.center.y + bfly_vy); } if(bfly.center.y > frameheight) { bfly.center = cgpointmake(bfly.center.x + bfly_vx, 0); } else if (bfly.center.y < 0) { bfly.center = cgpointmake(bfly.center.x + bfly_vx, frameheight); } }
-(void)movebutterfly{ static int dx = 1; static int dy = 1; if (bfly.frame.origin.x >= self.view.bounds.size.width - bfly.bounds.size.width) { dx = -dx; } if (bfly.frame.origin.y >= self.view.bounds.size.height - bfly.bounds.size.height) { dy = -dy; } if (bfly.frame.origin.x <= 0) { dx = -dx; } if (bfly.frame.origin.y <= 0) { dy = -dy; } cgpoint point = bfly.center; point.x += dx; point.y += dy; bfly.center = point; } keep calling function using nstimer @ rate want update position. here dx , dy velocity @ butterfly move.
Comments
Post a Comment