如何在cocos2d中移动CCNode中的CCSprite

时间:2014-07-26 00:49:44

标签: cocos2d-iphone

我有一个Bird(CCSprite),我希望它在屏幕顶部偏离航线,所以我添加了CCNode并添加了它作为孩子的鸟,但它仍在飞行之外CCNode!

我的代码是:

@implementation GamePlay {

    CCSprite *myBird;
    CCNode *theFlyingArea;
}

-(id)init {

    if(self=[super init]) {

       theFlyingArea = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:0.4f] width:viewSize.width height:viewSize.height*60/100];
       theFlyingArea.position = ccp(0, 0);
       theFlyingArea.positionType = CCPositionTypeMake(CCPositionUnitPoints, CCPositionUnitPoints, CCPositionReferenceCornerTopLeft);
       theFlyingArea.anchorPoint = ccp(0, 1);
       [self addChild:theFlyingArea z:1];

       float RandomY = arc4random() % (int)theFlyingArea.contentSize.height;

       // Create MY BIRD
       myBird = [CCSprite spriteWithImageNamed:@"myBird.png"];
       myBird.scale = myBird.scale / 2;
       myBird.position = ccp(-10, RandomY);
       [theFlyingArea addChild:myBird z:2];       

    }

    return self;
}

- (void)update:(CCTime)delta {

   id moveTo = [CCActionMoveTo actionWithDuration:100.f position:ccp(((float)rand() / RAND_MAX) * theFlyingArea.contentSize.width , ((float)rand() / RAND_MAX) * theFlyingArea.contentSize.height)];

   [myBird runAction:moveTo];
}

@end

非常感谢。

0 个答案:

没有答案