gpt4 book ai didi

ios - Sprite 套件 CGPoint 未按预期工作

转载 作者:搜寻专家 更新时间:2023-10-30 20:18:20 24 4
gpt4 key购买 nike

我正在用 Sprite Kit 编写游戏,但我在使用 CGPoint 时遇到问题,目前如果手机是纵向的,那么对象从屏幕右侧进入并向左移动,我需要它来从屏幕顶部进入并掉到底部,如何让它从屏幕顶部生成?目前它也向右行进,我想向下行进这是与“导弹”相关的所有代码..

static inline CGPoint CGPointAdd(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);
}



-(void)addMissile
{
//initalizing spaceship node
SKSpriteNode *missile;
missile = [SKSpriteNode spriteNodeWithImageNamed:@"red-missile.png"];
[missile setScale:.01];

//Adding SpriteKit physicsBody for collision detection
missile.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:missile.size];
missile.physicsBody.categoryBitMask = obstacleCategory;
missile.physicsBody.dynamic = YES;
missile.physicsBody.contactTestBitMask = shipCategory;
missile.physicsBody.collisionBitMask = 0;
missile.physicsBody.usesPreciseCollisionDetection = YES;
missile.name = @"missile";

//selecting random y position for missile
//int t = arc4random() % 300;
missile.position = CGPointMake(100, 100);

[self addChild:missile];
}



- (void)moveObstacle
{
NSArray *nodes = self.children;//1

for(SKNode * node in nodes){
if (![node.name isEqual: @"bg"] && ![node.name isEqual: @"ship"]) {
SKSpriteNode *ob = (SKSpriteNode *) node;
CGPoint obVelocity = CGPointMake(-OBJECT_VELOCITY, 0);
CGPoint amtToMove = CGPointMultiplyScalar(obVelocity,_dt);

ob.position = CGPointAdd(ob.position, amtToMove);
if(ob.position.y < 100)
{
[ob removeFromParent];
}
}
}
}

最佳答案

导弹动画很简单,所以在这种情况下您不需要GDPointAddMultiplyByScalar

如果你需要一个从上到下移动的导弹,这里是代码

- (void)shootMissile {
// Sprite Kit knows that we are working with images so we don't need to pass the image’s extension
SKSpriteNode *missile = [SKSpriteNode spriteNodeWithImageNamed:@"red-missile"];
// Position the missile outside the top
missile.position = CGPointMake(self.size.width/2, self.size.height + missile.size.height/2);
// Add the missile to the scene
[self addChild:missile];

// Here is the Magic
// Run a sequence
[missile runAction:[SKAction sequence:@[
// Move the missile and Specify the animation time
[SKAction moveByX:0 y:-(self.size.height + missile.size.height) duration:3],
// When the missile is outside the bottom
// The missile will disappear
[SKAction removeFromParent] ]
]];
}

你只需要在你想发射导弹时调用这个函数

现在您可以将所有物理添加到导弹

祝你好运!

关于ios - Sprite 套件 CGPoint 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20658061/

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