2014-07-22 4 views
0

SpriteKit 물리학을 사용하여 출발점을 기준으로 콘에서 발사체를 어떻게 쏠 수 있는지 궁금합니다. 현재 방금 다음과 같은 루프를 사용합니다 (처음 스 와이프를 중심으로하지 않음).콘에서 총알을 쏴

float distance = DistanceBetweenTwoPoints(_startSwipe, _endSwipe); 
float cballDx = (_endSwipe.x - _startSwipe.x)/distance; 
float cballDy = (_endSwipe.y - _startSwipe.y)/distance;  

for (int i = 0; i < playerShip.cannons; i++) { 
    SKSpriteNode *cannon = [SKSpriteNode spriteNodeWithImageNamed:@"cball"]; 
    cannon.name = @"cball"; 
    cannon.position = _touchedShip.position; 
    cannon.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:cannon.size.width * 0.5]; 
    cannon.physicsBody.dynamic = YES; 
    cannon.physicsBody.allowsRotation = NO; 
    cannon.physicsBody.friction = 0.0; 
    cannon.physicsBody.linearDamping = 0.0; 
    cannon.physicsBody.categoryBitMask = cannonCategory; 

    cannon.physicsBody.velocity = CGVectorMake(cballDx * 150 + i * 5, cballDy * 150); 
    [self addChild:cannon]; 
} 

도움이 매우 감사합니다! 이 밖에

답변

0

체크 그것은 당신이 당신의 게임에 DX와 DY 충동 ACC를 조정할 수 있습니다 작동합니다

#import "GameScene.h" 

@implementation GameScene 
{ 
    CGPoint startTouchPosition; 
    CGPoint currentTouchPosition; 
    SKSpriteNode *mysprite; 
} 
-(void)didMoveToView:(SKView *)view { 
    /* Setup your scene here */ 
    self.physicsBody.affectedByGravity=NO; 
    self.physicsWorld.gravity=CGVectorMake(0.0,-9.8); 
    self.physicsWorld.contactDelegate=self; 
    self.physicsWorld.speed=1.5; 
    self.speed=1.5; 
    [self createObject]; 
} 
-(void)createObject 
{ 
    mysprite=[SKSpriteNode spriteNodeWithImageNamed:@"bottle1.png"]; 
} 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 



} 

-(void)addspriteandjump:(CGVector)jumpPoint position:(CGPoint)point 
{ 
    SKSpriteNode *clone=mysprite.copy; 
    [self addChild:clone]; 
    clone.position=point; 
    clone.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:clone.size]; 
    clone.physicsBody.dynamic=YES; 
    clone.physicsBody.affectedByGravity=YES; 
    [email protected]"gameHero"; 
    clone.physicsBody.allowsRotation=YES; 
    [clone.physicsBody applyImpulse:jumpPoint]; 
} 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGVector jumpPoint; 
    UITouch *aTouch = [touches anyObject]; 
    currentTouchPosition= [aTouch locationInNode:self]; 
    SKNode *node = [self nodeAtPoint:startTouchPosition]; 
    jumpPoint.dy=100; 
    jumpPoint.dx=abs(currentTouchPosition.x-startTouchPosition.x)/16; 
    [self addspriteandjump:jumpPoint position:startTouchPosition]; 

    startTouchPosition = CGPointZero; 
    currentTouchPosition = CGPointZero; 

} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *aTouch = [touches anyObject]; 
    startTouchPosition= [aTouch locationInNode:self]; 

    SKNode *node = [self nodeAtPoint:startTouchPosition]; 
    } 

-(void)update:(CFTimeInterval)currentTime { 
    /* Called before each frame is rendered */ 
} 

@end 
관련 문제