ドラゴンフライングのポーズを含むスプライトシートがあり、それをプロジェクトにうまく統合し、画面上でうまく飛んでいます。x 軸と y 軸を介してドラゴンをランダムに移動するコードを書きましたが、ドラゴンがランダムに位置を後方に変更しても、ドラゴンは反転していません。ドラゴンが画面を回転させるときに、ドラゴンを裏返す必要があります。
このコードは、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;
}