Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
开发者_开发问答 Improve this questionIs there any alternative for CCMoveBy Action in cocos2d in iphone ??
If anybody know about this.. plz reply.
An alternative to using actions to move sprites is to use simple physics. You can extend the CCSprite class to include an update method as well as ivars / properties to keep track of x and y velocity.
In the update method, move the sprite by velocity * dT in each direction. Call the sprites update method in the update method of your game scene.
MovingSprite.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface MovingSprite : CCSprite {
float _vx;
float _vy;
}
-(void) update:(ccTime)dt;
@property (nonatomic, assign) float vx;
@property (nonatomic, assign) float vy;
@end
MovingSprite.m
#import "MovingSprite.h"
@implementation MovingSprite
@synthesize vx = _vx;
@synthesize vy = _vy;
-(void)update:(ccTime)dT
{
self.vy -= (kGravity * dT); //optionally apply gravity
self.position = ccp(self.position.x + (self.vx*dT), self.position.y + (self.vy*dT));
}
And add [self scheduleUpdate];
to the init method of your game layer. Then add an update method within the game layer where you call update for all moving sprites.
Now you just need to add collision detection to check if the car collides with the sides of the track.
精彩评论