Cocos2d iphone 使用Cocos2d不断移动的对象

Cocos2d iphone 使用Cocos2d不断移动的对象,cocos2d-iphone,Cocos2d Iphone,我正在寻找一些关于如何使用Cocos2d,我可以有一个物体(比如说火箭)以恒定的速度持续运动的指导,我有两个按钮来简单地改变它的方向 我已经找到了一些关于如何改变火箭方向的信息,但是我一直坚持让火箭持续运动 稍后,我想让火箭能够飞离屏幕并重新出现在屏幕的另一侧,但这是另一个问题(我已经找到了一些关于这方面的帮助) 我是Cocos2d的新手,已经搜索了几个小时,但还没有找到我想要的东西 有人能给我指出一个正确的方向,让我的火箭不断地移动,并对方向的变化做出反应吗?因此,这里是第一次介绍如何做到这一

我正在寻找一些关于如何使用Cocos2d,我可以有一个物体(比如说火箭)以恒定的速度持续运动的指导,我有两个按钮来简单地改变它的方向

我已经找到了一些关于如何改变火箭方向的信息,但是我一直坚持让火箭持续运动

稍后,我想让火箭能够飞离屏幕并重新出现在屏幕的另一侧,但这是另一个问题(我已经找到了一些关于这方面的帮助)

我是Cocos2d的新手,已经搜索了几个小时,但还没有找到我想要的东西


有人能给我指出一个正确的方向,让我的火箭不断地移动,并对方向的变化做出反应吗?

因此,这里是第一次介绍如何做到这一点:

//Projectletest拥有一个射弹类,它只知道如何 移动自身

//WrapBoundaryTest是显示 放映员

projectletest.h

#import "cocos2d.h"

@interface ProjectileTest : CCNode
{
    CCSprite *sprite;
    CGPoint velocity;
    CGRect bounds;
    BOOL isActive;
    float steerAmount;
}

@property CCSprite *sprite;
@property CGPoint velocity;
@property CGRect bounds;
@property BOOL isActive;
@property float steerAmount;

+(id) projectileWithBounds:(CGRect) boundary
               andVelocity:(CGPoint) v
         andSteeringAmount:(float) steeringAmount;

-(void) step:(CCTime) dt;
-(void) steerLeft;
-(void) steerRight;
@end
#import "cocos2d.h"
#import "cocos2d-ui.h"

@interface WrapBoundaryTest : CCScene
{
    NSMutableArray *projectiles;
    CCButton *left;
    CCButton *right;
}

@property __strong NSMutableArray *projectiles;
+(id) scene;
@end
projectletest.m

#import "ProjectileTest.h"

@implementation ProjectileTest

@synthesize sprite, velocity, bounds, isActive, steerAmount;

+(id) projectileWithBounds:(CGRect) boundary andVelocity:(CGPoint) v andSteeringAmount:(float) steeringAmount
{
    ProjectileTest *proj = [[self alloc] init];
    proj.bounds = boundary;
    proj.velocity = v;
    proj.steerAmount = steeringAmount;
    proj.isActive = YES;
    [proj calculateAngleForVelocity: v];
    return proj;
}

-(id) init
{
    if(( self = [super init]))
    {
        sprite = [CCSprite spriteWithImageNamed:@"Icon.png"];
        [self addChild:sprite];

        [sprite setScale:0.50f];
        bounds = CGRectZero;
        velocity = CGPointZero;
        isActive = NO;
        steerAmount = 1.0f;
    }
    return self;
}

-(void) calculateAngleForVelocity:(CGPoint) v
{
    float rads = ccpToAngle(v);
    float degs = -CC_RADIANS_TO_DEGREES(rads);
    sprite.rotation = degs + 90.0f;
}

-(void) steerLeft
{
    [self steer: YES];
}

-(void) steerRight
{
    [self steer: NO];
}

-(void) steer:(BOOL) left
{
    if(left)
    {
        velocity = ccpRotateByAngle(velocity, CGPointZero, -CC_DEGREES_TO_RADIANS(-steerAmount));
    }
    else // right
    {
        velocity = ccpRotateByAngle(velocity, CGPointZero, -CC_DEGREES_TO_RADIANS(steerAmount));
    }
}

-(void) step:(CCTime)dt
{
    if(isActive)
    {
        if(CGRectContainsPoint(bounds, self.position))
        {
            self.position = ccpAdd(self.position, velocity);
            [self calculateAngleForVelocity: velocity];
        }
        else
        {
            float nudge = 0.5f;
            if(self.position.x >= bounds.size.width && velocity.x > 0.0f)
            {
                self.position = ccp(bounds.origin.x + nudge, self.position.y);
            }
            else if(self.position.x <= bounds.origin.x && velocity.x < 0.0f)
            {
                self.position = ccp(bounds.size.width - nudge, self.position.y);
            }

            if(self.position.y >= bounds.size.height && velocity.y > 0.0f)
            {
                self.position = ccp(self.position.x, bounds.origin.y + nudge);
            }
            else if(self.position.y <= bounds.origin.y && velocity.y < 0.0f)
            {
                self.position = ccp(self.position.x, bounds.size.height - nudge);
            }
        }
    }
}

@end
#import "WrapBoundaryTest.h"
#import "ProjectileTest.h"

@implementation WrapBoundaryTest

@synthesize projectiles;

+(id) scene
{
    return [[self alloc] init];
}

-(id) init
{
    if(( self = [super init]))
    {
        projectiles = [NSMutableArray array];
        CGRect projectileBounds = CGRectMake(
                                             0.0f,
                                             0.0f,
                                             [CCDirector sharedDirector].designSize.width,
                                             [CCDirector sharedDirector].designSize.height
                                             );

        CGPoint origin = ccp(200.0f, 150.0f);
        [self addProjectileWithBoundsRect:projectileBounds andVelocity:ccp( 1.0f,  5.0f) andSteeringAmount:2.0f atStartingPosition:origin];
        [self addProjectileWithBoundsRect:projectileBounds andVelocity:ccp(-1.0f, -5.0f) andSteeringAmount:1.0f atStartingPosition:origin];
        [self addProjectileWithBoundsRect:projectileBounds andVelocity:ccp(-1.0f,  1.0f) andSteeringAmount:0.5f atStartingPosition:origin];
        [self addProjectileWithBoundsRect:projectileBounds andVelocity:ccp( 8.0f,  1.0f) andSteeringAmount:7.5f atStartingPosition:origin];

        left = [CCButton buttonWithTitle:@"Left" fontName:@"Arial" fontSize:16.0f];
        left.positionType = CCPositionTypeNormalized;
        left.position = ccp(0.1f, 0.1f);

        [self addChild:left];

        right = [CCButton buttonWithTitle:@"Right" fontName:@"Arial" fontSize:16.0f];
        right.positionType = CCPositionTypeNormalized;
        right.position = ccp(0.9f, 0.1f);
        [self addChild:right];
    }
    return self;
}

-(void) addProjectileWithBoundsRect:(CGRect) boundsRect
                        andVelocity:(CGPoint) velocity
                  andSteeringAmount:(float) steeringAmount
                 atStartingPosition:(CGPoint) startingPosition
{
    ProjectileTest *proj = [ProjectileTest projectileWithBounds: boundsRect
                                                    andVelocity: velocity
                                              andSteeringAmount: steeringAmount
                            ];
    proj.position = startingPosition;
    [self addChild:proj];
    [projectiles addObject:proj];
}

-(void) update:(CCTime)delta
{
    for(ProjectileTest *p in projectiles)
    {
        if(left.touchInside)
        {
            [p steerLeft];
        }

        if(right.touchInside)
        {
            [p steerRight];
        }

        [p step:delta];
    }
}

@end
WrapBoundaryTest.m

#import "ProjectileTest.h"

@implementation ProjectileTest

@synthesize sprite, velocity, bounds, isActive, steerAmount;

+(id) projectileWithBounds:(CGRect) boundary andVelocity:(CGPoint) v andSteeringAmount:(float) steeringAmount
{
    ProjectileTest *proj = [[self alloc] init];
    proj.bounds = boundary;
    proj.velocity = v;
    proj.steerAmount = steeringAmount;
    proj.isActive = YES;
    [proj calculateAngleForVelocity: v];
    return proj;
}

-(id) init
{
    if(( self = [super init]))
    {
        sprite = [CCSprite spriteWithImageNamed:@"Icon.png"];
        [self addChild:sprite];

        [sprite setScale:0.50f];
        bounds = CGRectZero;
        velocity = CGPointZero;
        isActive = NO;
        steerAmount = 1.0f;
    }
    return self;
}

-(void) calculateAngleForVelocity:(CGPoint) v
{
    float rads = ccpToAngle(v);
    float degs = -CC_RADIANS_TO_DEGREES(rads);
    sprite.rotation = degs + 90.0f;
}

-(void) steerLeft
{
    [self steer: YES];
}

-(void) steerRight
{
    [self steer: NO];
}

-(void) steer:(BOOL) left
{
    if(left)
    {
        velocity = ccpRotateByAngle(velocity, CGPointZero, -CC_DEGREES_TO_RADIANS(-steerAmount));
    }
    else // right
    {
        velocity = ccpRotateByAngle(velocity, CGPointZero, -CC_DEGREES_TO_RADIANS(steerAmount));
    }
}

-(void) step:(CCTime)dt
{
    if(isActive)
    {
        if(CGRectContainsPoint(bounds, self.position))
        {
            self.position = ccpAdd(self.position, velocity);
            [self calculateAngleForVelocity: velocity];
        }
        else
        {
            float nudge = 0.5f;
            if(self.position.x >= bounds.size.width && velocity.x > 0.0f)
            {
                self.position = ccp(bounds.origin.x + nudge, self.position.y);
            }
            else if(self.position.x <= bounds.origin.x && velocity.x < 0.0f)
            {
                self.position = ccp(bounds.size.width - nudge, self.position.y);
            }

            if(self.position.y >= bounds.size.height && velocity.y > 0.0f)
            {
                self.position = ccp(self.position.x, bounds.origin.y + nudge);
            }
            else if(self.position.y <= bounds.origin.y && velocity.y < 0.0f)
            {
                self.position = ccp(self.position.x, bounds.size.height - nudge);
            }
        }
    }
}

@end
#import "WrapBoundaryTest.h"
#import "ProjectileTest.h"

@implementation WrapBoundaryTest

@synthesize projectiles;

+(id) scene
{
    return [[self alloc] init];
}

-(id) init
{
    if(( self = [super init]))
    {
        projectiles = [NSMutableArray array];
        CGRect projectileBounds = CGRectMake(
                                             0.0f,
                                             0.0f,
                                             [CCDirector sharedDirector].designSize.width,
                                             [CCDirector sharedDirector].designSize.height
                                             );

        CGPoint origin = ccp(200.0f, 150.0f);
        [self addProjectileWithBoundsRect:projectileBounds andVelocity:ccp( 1.0f,  5.0f) andSteeringAmount:2.0f atStartingPosition:origin];
        [self addProjectileWithBoundsRect:projectileBounds andVelocity:ccp(-1.0f, -5.0f) andSteeringAmount:1.0f atStartingPosition:origin];
        [self addProjectileWithBoundsRect:projectileBounds andVelocity:ccp(-1.0f,  1.0f) andSteeringAmount:0.5f atStartingPosition:origin];
        [self addProjectileWithBoundsRect:projectileBounds andVelocity:ccp( 8.0f,  1.0f) andSteeringAmount:7.5f atStartingPosition:origin];

        left = [CCButton buttonWithTitle:@"Left" fontName:@"Arial" fontSize:16.0f];
        left.positionType = CCPositionTypeNormalized;
        left.position = ccp(0.1f, 0.1f);

        [self addChild:left];

        right = [CCButton buttonWithTitle:@"Right" fontName:@"Arial" fontSize:16.0f];
        right.positionType = CCPositionTypeNormalized;
        right.position = ccp(0.9f, 0.1f);
        [self addChild:right];
    }
    return self;
}

-(void) addProjectileWithBoundsRect:(CGRect) boundsRect
                        andVelocity:(CGPoint) velocity
                  andSteeringAmount:(float) steeringAmount
                 atStartingPosition:(CGPoint) startingPosition
{
    ProjectileTest *proj = [ProjectileTest projectileWithBounds: boundsRect
                                                    andVelocity: velocity
                                              andSteeringAmount: steeringAmount
                            ];
    proj.position = startingPosition;
    [self addChild:proj];
    [projectiles addObject:proj];
}

-(void) update:(CCTime)delta
{
    for(ProjectileTest *p in projectiles)
    {
        if(left.touchInside)
        {
            [p steerLeft];
        }

        if(right.touchInside)
        {
            [p steerRight];
        }

        [p step:delta];
    }
}

@end

//HTH

您使用的是哪一版本的cocos2d?我目前使用的是3 rc 5