【问题标题】:Shoot bullets in a cone用锥形射击子弹
【发布时间】:2014-07-22 23:54:36
【问题描述】:

我想知道如何使用 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];
}

非常感谢任何帮助!

【问题讨论】:

    标签: sprite-kit game-physics projectile


    【解决方案1】:

    看看这个,你可以根据你的游戏调整 dx 和 dy 冲动

    #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;
        clone.name=@"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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多