[英]SpriteKit app crash when running [SKAction moveByX:y:duration]
[英]SpriteKit moveByX half works
我有一個SKSpriteNode作為SKScene的子級,但是sprite節點比場景大得多,並且我需要用戶能夠手動滾動sprite節點以查看全部(這是一張巨大的地圖) )。 我正在使用SKAction moveByX:y:持續時間:方法,並且moveByX部分按預期工作。 但是,按Y pat的移動會使地圖非常快地從屏幕底部射出。 這是我在touchesMoved中的代碼:
CGFloat xDiff = location.x - prevLocation.x;
CGFloat yDiff = location.y - prevLocation.y;
SKAction *moveBy = [SKAction moveByX:xDiff y:yDiff duration:0];
if (YES) NSLog(@"%f, %f", xDiff, yDiff);
[map runAction:moveBy];
因此,對於x軸,此邏輯是正確的,但對於y軸,則不是這樣。 此外,如果我將yDiff更改為反向計算(prevLocation.y-location.y),我會在NSLog輸出中注意到yDiff是在單個pan操作中累積的,但xDiff不是。
我想念什么?
我嘗試過為您編寫代碼,它可以實現您的期望。 只需仔細檢查一下移動的觸摸效果就象這樣。
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint currentPoint = [[touches anyObject] locationInNode:self];
CGPoint previousPoint = [[touches anyObject] previousLocationInNode:self];
CGFloat xDiff = currentPoint.x - previousPoint.x;
CGFloat yDiff = currentPoint.y - previousPoint.y;
NSLog(@"%f, %f", xDiff, yDiff);
SKAction *moveBy = [SKAction moveByX:xDiff y:yDiff duration:0];
[_bg runAction:moveBy];
}
我也這樣設置背景。
_bg = [SKSpriteNode spriteNodeWithImageNamed:@"background"];
_bg.anchorPoint = CGPointZero;
_bg.position = CGPointZero;
[self addChild:_bg];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.