cocos2d移动精灵?

时间:2013-04-04 19:28:52

标签: cocos2d-iphone

需要一些建议/指出正确的方向来解决这个问题..学校的小伙子..不是作业!个人学习。我无法弄清楚如何让spites从上到下。下面的代码使它们从左到右。提前致谢。

 CCSprite * monster = [CCSprite spriteWithFile:@"image.png"];
// Determine where to spawn the monster along the Y axis
CGSize winSize = [CCDirector sharedDirector].winSize;
int minX = monster.contentSize.width/ 0.2;
int maxX = winSize.height - monster1.contentSize.width/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;

// Create the monster slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
monster.position = ccp(winSize1.width + monster.contentSize.width/2, actualX);
[self addChild:monster1];

// Determine speed of the monster
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;

// Create the actions
CCMoveTo * actionMove = [CCMoveTo actionWithDuration:actualDuration
                                            position:ccp(-monster.contentSize.width/2, actualX)];
CCCallBlockN * actionMoveDone = [CCCallBlockN actionWithBlock:^(CCNode *node) {
    [node removeFromParentAndCleanup:YES];
}];
[monster1 runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

1 个答案:

答案 0 :(得分:1)

这句话说:

CCMoveTo * actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(-monster.contentSize.width/2, actualX)];

是什么设置运动。您告诉精灵移动到的位置ccp(-monster.contentSize.width/2, actualX)是您想要移动到的(X,Y)坐标。 X是第一个值,Y是第二个值。出于某种原因,您希望移动到的Y值为actualX

如果要向上或向下移动,则应将精灵移动到新的Y坐标。也许是这样的?

CCMoveTo * actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(monster.position.x, monster.position.y - 300)];