1
我有一個精靈表,其中包含龍飛行的姿勢,我已經成功地將它集成到我的項目中,並且它在我的屏幕上飛得很好。我編寫了通過x軸和y軸隨機移動龍的代碼,但是當龍隨機將位置改變爲向後時,它不會向後翻轉,這意味着當龍向後移動時,它的表面仍然位於後部。當它轉動屏幕時,我需要翻轉龍。cocos2d中的Sprite Sheet動畫iOS
此代碼是隨機移動到x和y軸的代碼。
-(void)moveRandom:(CCSprite*)s
{
CGPoint randomPoint = ccp(arc4random()%2000, arc4random()%500);
NSLog(@"%@", NSStringFromCGPoint(randomPoint));
[s runAction:
[CCSequence actions:
[CCMoveTo actionWithDuration:arc4random()%5+1 position: randomPoint],
[CCCallBlock actionWithBlock:^{
[self performSelector:@selector(moveRandom:) withObject:s afterDelay:0.5];
}],
nil]
];
}
以下代碼是在init方法
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @"dr.plist"];
CCSpriteBatchNode *parrotSheet = [CCSpriteBatchNode batchNodeWithFile:@"dr.png"];
[self addChild:parrotSheet];
NSMutableArray *flyAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 6; ++i) {
[flyAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"dragon%d.png", i]]];
}
[self moveRandom:theParrot];
CCAction *flyAction = [CCRepeatForever actionWithAction:
[CCAnimate actionWithAnimation:flyAnim restoreOriginalFrame:NO]];
//start the action
[theParrot runAction:flyAction];
//add the sprite to the CCSpriteBatchNode object
[parrotSheet addChild:theParrot];
如何解決此問題。? ...
我有另一個示例應用程序,它會翻轉圖像加入屏幕中的觸摸。當我們觸摸屏幕左側時,圖像會左轉並移動該方向。 我需要這個,但沒有聯繫,自動想要轉動圖像。
這個代碼是
-(id) init {
if((self = [super init])) {
// This loads an image of the same name (but ending in png), and goes through the
// plist to add definitions of each frame to the cache.
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"AnimBear.plist"];
// Create a sprite sheet with the Happy Bear images
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"AnimBear.png"];
[self addChild:spriteSheet];
// Load up the frames of our animation
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 8; ++i) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"bear%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
// Create a sprite for our bear
CGSize winSize = [CCDirector sharedDirector].winSize;
self.bear = [CCSprite spriteWithSpriteFrameName:@"bear1.png"];
_bear.position = ccp(winSize.width/2, winSize.height/2);
self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
//[_bear runAction:_walkAction];
[spriteSheet addChild:_bear];
self.isTouchEnabled = YES;
}
return self;
}
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
float bearVelocity = 480.0/3.0;
CGPoint moveDifference = ccpSub(touchLocation, _bear.position);
float distanceToMove = ccpLength(moveDifference);
float moveDuration = distanceToMove/bearVelocity;
if (moveDifference.x < 0) {
_bear.flipX = NO;
} else {
_bear.flipX = YES;
}
[_bear stopAction:_moveAction];
if (!_moving) {
[_bear runAction:_walkAction];
}
self.moveAction = [CCSequence actions:
[CCMoveTo actionWithDuration:moveDuration position:touchLocation],
[CCCallFunc actionWithTarget:self selector:@selector(bearMoveEnded)],
nil
];
[_bear runAction:_moveAction];
_moving = TRUE;
}
-(void)bearMoveEnded {
[_bear stopAction:_walkAction];
_moving = FALSE;
}
,,喔,謝謝,它的工作原理fine..thanks非常 – stackiphone 2012-07-09 11:15:11