Reputation: 1245
I have a sprite sheet which contain poses for dragon flying, I have successfully integrated it into my project and it flys well on my screen. I wrote the code for moving the dragon randomly through x and y axis, but when the dragon randomly changes the postion to backward it is not flipping back, meaning that when the dragon moves backward its face is still in the back portion. I need to flip the dragon when it turns the screen.
This code is the code for randomly moving to x and y axis.
-(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]
];
}
The following code is in the init method
[[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];
how to solve this.? ...
I have another sample app, that will flip the image acceding to a touch in the screen. when we touch the left of the screen the image will turn left and move that direction. I need like this, but not in touch, automatically want to turn the image.
the code for this is
-(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;
}
Upvotes: 1
Views: 788
Reputation: 3751
It's exactly the same idea as that of the sample code you showed.
Whenever you generate a new random position for your dragon to fly to, you need to check whether it is to the left or right of your current dragon position, and then flip the dragon sprite accordingly to face that direction by setting the flipX property:
-(void)moveRandom:(CCSprite*)s
{
CGPoint randomPoint = ccp(arc4random()%2000, arc4random()%500);
NSLog(@"%@", NSStringFromCGPoint(randomPoint));
CGPoint moveDifference = ccpSub(randomPoint, s.position);
if (moveDifference.x < 0)
{
s.flipX = NO;
}
else
{
s.flipX = YES;
}
// the rest of your code...
}
Upvotes: 1