2014-04-14 92 views
1

嗨,大家好我有這個雪碧套件大炮遊戲,它完美的作品,但有一個問題。如果我將大炮指向頂部或底部,就像您在圖像中看到的那樣,子彈由大炮的精靈中心產生,它清楚地表明子彈不是從大炮的隧道中被射出,而是從圖像的中心,我怎樣才能修復這個子彈的位置,使它始終從隧道的口中產生。無法得到子彈圖像產卵在正確的位置

這是我的代碼到目前爲止。

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
     CGPoint location = [_Player position]; 
     UITouch *touch = [touches anyObject]; 
     CGPoint touchLocation = [touch locationInNode:self.scene]; 
     bullet = [SKSpriteNode spriteNodeWithImageNamed:@"cannonbullet"]; 
     bullet.xScale = 0.06; 
     bullet.yScale = 0.06; 
     bullet.position = 
     CGPointMake(location.x+_Player.zRotation,location.y+_Player.zRotation); 
     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]]]; 
     [self animStarter]; 
} 

正如你可以在圖像中看到的,如果大炮沒有直射,炮彈將從非常偏離的位置產生。

enter image description here

回答

1

確保您的大炮的炮筒精靈圖像排列有圖像的中心X軸(見圖)。如果要複製項目,可以拖出或複製圖像。

下面是將大炮旋轉到觸摸位置並在炮彈達到所需旋轉角度後發射炮彈的代碼。我認爲你仍然在使用殭屍conga代碼,所以我使用它的功能爲了您的方便。

enter image description here

#import "MyScene.h" 

static inline CGPoint CGPointSubtract(const CGPoint a, 
            const CGPoint b) 
{ 
    return CGPointMake(a.x - b.x, a.y - b.y); 
} 

static inline CGPoint CGPointMultiplyScalar(const CGPoint a,const CGFloat b) 
{ 
    return CGPointMake(a.x * b, a.y * b); 
} 

static inline CGFloat CGPointLength(const CGPoint a) 
{ 
    return sqrtf(a.x * a.x + a.y * a.y); 
} 

static inline CGPoint CGPointNormalize(const CGPoint a) 
{ 
    CGFloat length = CGPointLength(a); 
    return CGPointMake(a.x/length, a.y/length); 
} 

static inline CGFloat CGPointToAngle(const CGPoint a) 
{ 
    return atan2f(a.y, a.x); 
} 

static inline CGFloat ScalarSign(CGFloat a) 
{ 
    return a >= 0 ? 1 : -1; 
} 

static inline CGFloat ScalarShortestAngleBetween(const CGFloat a, const CGFloat b) 
{ 
    CGFloat difference = b - a; 
    CGFloat angle = fmodf(difference, M_PI * 2); 
    if (angle >= M_PI) { 
     angle -= M_PI * 2; 
    } 
    return angle; 
} 

static const float ROTATE_RADIANS_PER_SEC = 4 * M_PI; 
static const float MOVE_POINTS_PER_SEC = 120.0; 

@implementation MyScene 
{ 
    SKSpriteNode *cannon; 
    NSTimeInterval _lastUpdateTime; 
    NSTimeInterval _dt; 
    CGPoint _velocity; 
    CGPoint _lastTouchLocation; 
    BOOL fireCannon; 
    CGPoint destination; 
} 

-(id)initWithSize:(CGSize)size 
{ 
    if (self = [super initWithSize:size]) 
    { 
     [self createCannon]; 
     fireCannon = false; 
    } 
    return self; 
} 


-(void)createCannon 
{ 
    cannon = [SKSpriteNode spriteNodeWithImageNamed:@"cannon"]; 
    cannon.position = CGPointMake(self.size.height/2, self.size.width/2); 
    [self addChild:cannon]; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchLocation = [touch locationInNode:self.scene]; 

    fireCannon = true; 
    destination = touchLocation; 

    _lastTouchLocation = touchLocation; 
    CGPoint offset = CGPointSubtract(touchLocation, cannon.position); 

    CGPoint direction = CGPointNormalize(offset); 
    _velocity = CGPointMultiplyScalar(direction, MOVE_POINTS_PER_SEC); 
} 

-(void)update:(CFTimeInterval)currentTime 
{ 
    if (_lastUpdateTime) { 
     _dt = currentTime - _lastUpdateTime; 
    } else { 
     _dt = 0; 
    } 
    _lastUpdateTime = currentTime; 

    [self rotateSprite:cannon toFace:_velocity rotateRadiansPerSec:ROTATE_RADIANS_PER_SEC]; 
} 


- (void)rotateSprite:(SKSpriteNode *)sprite 
       toFace:(CGPoint)velocity 
    rotateRadiansPerSec:(CGFloat)rotateRadiansPerSec 
{ 
    float targetAngle = CGPointToAngle(velocity); 
    float shortest = ScalarShortestAngleBetween(cannon.zRotation, targetAngle); 
    float amtToRotate = rotateRadiansPerSec * _dt; 
    if (ABS(shortest) < amtToRotate) 
    { 
     amtToRotate = ABS(shortest); 
    } 
    sprite.zRotation += ScalarSign(shortest) * amtToRotate; 

    if ((ABS(shortest) == amtToRotate) && (fireCannon == true)) 
    { 
     fireCannon = false; 
     [self fire:targetAngle]; 
    } 
} 

-(void)fire:(float)targetAngle 
{ 

    SKSpriteNode *cannonBall = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(5, 5)]; 
    cannonBall.position = cannon.position; 
    [self addChild:cannonBall]; 

    int x = cannon.position.x + 1000 * cos(targetAngle); 
    int y = cannon.position.y + 1000 * sin(targetAngle); 

    [cannonBall runAction:[SKAction moveTo:CGPointMake(x, y) duration:2]]; 
} 

@end 
+1

我就買午餐,如果我能見到你。 「」「」你真棒。「」「」這是作品。我不知道該如何謝謝你。但是,謝謝你一百萬次。 – Ray

+0

不客氣。繼續編碼! – sangony