开发者

Is there any alternative for CCMoveBy Action in cocos2d in iphone [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

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 question

Is 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜