我想知道你如何使用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 :(得分:0)
看看它会起作用你可以根据你的游戏调整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