gpt4 book ai didi

sprite-kit - 从旋转体射击

转载 作者:行者123 更新时间:2023-12-02 03:36:43 24 4
gpt4 key购买 nike

我有一个旋转的敌人 body ,可以发射子弹。它工作得很好,但它似乎会向所有方向开火,除了直接向下。我已经有一段时间没有尝试了,我想我忘记了什么。我必须检查敌人的旋转吗?

        SKAction *shoot = [SKAction moveTo:CGPointMake(2000*cosf(enemy.zRotation),2000*sinf(enemy.zRotation)) duration:5];
SKAction *remove = [SKAction removeFromParent];

[bullet runAction:[SKAction sequence:@[shoot,remove]]];

最佳答案

这是一艘可以 360 度旋转并从任何角度开火的船的一些代码。触摸屏幕左侧轻微旋转飞船,触摸屏幕右侧进行射击。

@implementation MyScene
{
SKSpriteNode *ship1;
SKShapeNode *beam1;
}

-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
//self.physicsWorld.contactDelegate = self;
[self createSpaceships];
}
return self;
}


-(void)createSpaceships
{
ship1 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(50, 50)];
ship1.position = CGPointMake(300, 150);
ship1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ship1.size];
ship1.physicsBody.dynamic = NO;
[self addChild:ship1];

SKSpriteNode *frontOfShip = [SKSpriteNode spriteNodeWithColor:[SKColor grayColor] size:CGSizeMake(2, 50)];
frontOfShip.position = CGPointMake(25, 0);
[ship1 addChild:frontOfShip];
}

// touch the left side of the screen to rotate the ship by +0.0785398 radians
// touch the right hand side of the screen to fire laser

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

if(touchLocation.x < self.size.width/2) // left side of screen
{
SKAction *block0 = [SKAction runBlock:^{
ship1.zRotation = ship1.zRotation +0.0785398;
}];
[self runAction:block0];
} else // right side of screen
{
int x = ship1.position.x + 1000 * cos(ship1.zRotation);
int y = ship1.position.y + 1000 * sin(ship1.zRotation);

beam1 = [SKShapeNode node];
CGMutablePathRef pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, ship1.position.x, ship1.position.y);
CGPathAddLineToPoint(pathToDraw, NULL, x, y);
beam1.path = pathToDraw;
[beam1 setStrokeColor:[UIColor redColor]];
[self addChild:beam1];
}
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//[beam1 removeFromParent];
}

-(void)update:(CFTimeInterval)currentTime
{
//
}

@end

关于sprite-kit - 从旋转体射击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22920495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com