大家好,我正在使用他为“僵尸康加”购买的书中的Ray Wenderlich代码。好吧,他让僵尸四处移动,这就是为什么让他的[自我启动动画]确实有意义;函数在movePlayerForward方法中运行。但我的问题是我的播放器没有移动,但拍摄停留在同一个位置,但我有拍摄动画,但它没有正常运行。那么我应该把[self startAnimatin] cal和[self stopAnimationCall]放在我编辑的代码中我很困惑。
- (void) Player {
_Player = [SKSpriteNode spriteNodeWithImageNamed:@"player1"];
_Player.xScale = 0.09;
_Player.yScale = 0.09;
_Player.position = CGPointMake(self.size.width/4, self.size.height/2);
_Player.zPosition = 1;
[self addChild:_Player];
// 1
NSMutableArray *PlayerCh = [NSMutableArray arrayWithCapacity:10];
// 2
for (int i = 1; i < 3; i++) {
NSString *mytextureName = [NSString stringWithFormat:@"player%d", i];
SKTexture *ItsTexture = [SKTexture textureWithImageNamed:mytextureName];
[PlayerCh addObject:ItsTexture];
}
// 3
for (int i = 3; i > 1; i--) {
NSString *mytextureName = [NSString stringWithFormat:@"player%d", i];
SKTexture *ItsTexture = [SKTexture textureWithImageNamed:mytextureName];
[PlayerCh addObject:ItsTexture];
}
// 4
_PlayerAnimation = [SKAction animateWithTextures:PlayerCh timePerFrame:0.1];
}
- (void) startPlayerAnimation {
if (![_Player actionForKey:@"animation"]) {
[_Player runAction:[SKAction repeatActionForever:_PlayerAnimation]
withKey:@"animation"];
}
}
- (void) stopPlayerAnimation {
[_Player removeActionForKey:@"animation"];
}
- (void) update:(NSTimeInterval)currentTime {
if (_lastUpdateTime) {
_dt = currentTime - _lastUpdateTime;
}
else {
_dt = 0;
}
_lastUpdateTime = currentTime;
CGPoint offset = CGPointSubtract(_lastTouchLocation, _Player.position);
float distance = CGPointLength(offset);
if (distance < PLAYER_MOVE_POINTS_PER_SEC * _dt) {
_velocity = CGPointZero;
}
else
{
[self rotateSprite:_Player toFace:_velocity
rotateRadiansPerSec:PLAYER_ROTATE_RADIANS_PER_SEC*_dt*2];
[self stopPlayerAnimation];
//
}
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
[self movePlayerToward:touchLocation];
[self startPlayerAnimation];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
[self movePlayerToward:touchLocation];
SKSpriteNode *bullet = [SKSpriteNode spriteNodeWithImageNamed:@"mybullet"];
bullet.xScale = 0.08;
bullet.yScale = 0.08;
bullet.position = _Player.position;
bullet.zPosition = 0;
CGPoint offset = rwSub(touchLocation, bullet.position);
if (offset.x <= 0) return;
[self addChild:bullet];
CGPoint direction = rwNormalize(offset);
CGPoint shootAmount = rwMult(direction, 400);
CGPoint realDest = rwAdd(shootAmount, bullet.position);
float velocity = 480.0/1.0;
float realMoveDuration = self.size.width / velocity;
SKAction * actionMove = [SKAction moveTo:realDest duration:realMoveDuration];
SKAction * actionMoveDone = [SKAction removeFromParent];
[bullet runAction:[SKAction sequence:@[actionMove, actionMoveDone]]];
}