Sprite 套件 CGPoint 未按预期工作
Posted
技术标签:
【中文标题】Sprite 套件 CGPoint 未按预期工作【英文标题】:Sprite kit CGPoint not working as expected 【发布时间】:2013-12-18 12:20:29 【问题描述】:我正在用 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];
【问题讨论】:
【参考方案1】:导弹动画很简单,所以在这种情况下您不需要 GDPointAdd 和 MultiplyByScalar
如果您需要从上到下移动的导弹,这里是代码
- (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] ]
]];
你只需要在你想发射导弹时调用这个函数
现在您可以将所有物理特性添加到导弹
祝你好运!!
【讨论】:
如何更新?我目前的方法不起作用 如果你从 touchesBegan 调用 shootMissile,每次你触摸屏幕都会出现一个导弹如果你在 update 里面调用它,每秒钟都会出现一个导弹。或者更新它是什么意思?给我更多信息,我会帮你的【参考方案2】:试试这个代码:
- (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(0, -OBJECT_VELOCITY);
CGPoint amtToMove = CGPointMultiplyScalar(obVelocity,_dt);
ob.position = CGPointAdd(ob.position, amtToMove);
if(ob.position.y < 0)
[ob removeFromParent];
虽然我不知道_dt
代表什么。
【讨论】:
以上是关于Sprite 套件 CGPoint 未按预期工作的主要内容,如果未能解决你的问题,请参考以下文章
react-redux s-s-r 异步 api 调用未按预期工作