你好, 我是 spriteKit 的新手,我正在尝试制作一个游戏。在游戏中,我有一个玩家从一个楼梯跳到另一个楼梯,它来自屏幕顶部无限(就像在 Doodle Jump 中一样,只有跳跃是由玩家的触摸控制的)。我试图通过对玩家施加脉冲来进行跳跃,但我想通过玩家的触摸持续时间来控制跳跃强度。我怎样才能这样做呢?当玩家开始触摸屏幕时执行跳跃,因此我无法测量跳跃强度(通过计算触摸持续时间)...有什么想法吗? 提前致谢!!!(:
最佳答案
这是一个简单的演示,用于在触摸持续时间的节点上应用脉冲。方法很简单:在触摸开始时设置一个 BOOL 变量 YES
,在触摸结束时设置 NO
。触摸时,它会在 update
方法中应用一个恒定的脉冲。
为了使游戏更加自然,您可能需要细化冲动 Action ,或者在节点上升时向下滚动背景。
GameScene.m:
#import "GameScene.h"
@interface GameScene ()
@property (nonatomic) SKSpriteNode *node;
@property BOOL touchingScreen;
@property CGFloat jumpHeightMax;
@end
@implementation GameScene
- (void)didMoveToView:(SKView *)view
{
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
// Generate a square node
self.node = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50.0, 50.0)];
self.node.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
self.node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.node.size];
self.node.physicsBody.allowsRotation = NO;
[self addChild:self.node];
}
const CGFloat kJumpHeight = 150.0;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.touchingScreen = YES;
self.jumpHeightMax = self.node.position.y + kJumpHeight;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.touchingScreen = NO;
self.jumpHeightMax = 0;
}
- (void)update:(CFTimeInterval)currentTime
{
if (self.touchingScreen && self.node.position.y <= self.jumpHeightMax) {
self.node.physicsBody.velocity = CGVectorMake(0, 0);
[self.node.physicsBody applyImpulse:CGVectorMake(0, 50)];
} else {
self.jumpHeightMax = 0;
}
}
@end
关于ios - 如何测量 Sprite 的跳跃强度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32540312/