Cocos2d iphone 如何在cocos2d中的CCNode内移动CCSprite
我有一只鸟,它是一个(CCSprite),我想让它飞到屏幕顶部偏离航线,所以我添加了CCNode,并将鸟作为子对象添加到它上面,但它仍然在CCNode之外飞行 我的代码是:Cocos2d iphone 如何在cocos2d中的CCNode内移动CCSprite,cocos2d-iphone,Cocos2d Iphone,我有一只鸟,它是一个(CCSprite),我想让它飞到屏幕顶部偏离航线,所以我添加了CCNode,并将鸟作为子对象添加到它上面,但它仍然在CCNode之外飞行 我的代码是: @implementation GamePlay { CCSprite *myBird; CCNode *theFlyingArea; } -(id)init { if(self=[super init]) { theFlyingArea = [CCNodeColor nodeW
@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
非常感谢。您是否希望精灵的位置限制为CCNodeColor的大小?这不会自动发生。您需要使用Physical为鸟创建屏幕大小的边和多边形形状的physicsBody,或者在更新中:通过将位置夹紧到所需的范围,验证moveTo位置不在屏幕/颜色节点之外。