如何测量精灵的跳跃强度?
How can I measure sprite's jump strength?
您好,
我是 spriteKit 的新手,我正在尝试制作游戏。在游戏中,我有一个从一个楼梯跳到另一个楼梯的玩家,它从屏幕的顶部无限跳跃(就像在涂鸦跳跃中一样,只有跳跃是由玩家的触摸控制的)。我试图通过对玩家施加冲动来跳跃,但我想通过玩家的触摸持续时间来控制跳跃强度。我该怎么做?当玩家 STARTS 触摸屏幕时执行跳跃,所以我无法测量跳跃强度(通过计算触摸持续时间)...有什么想法吗?
提前致谢!!! (:
这里有一个简单的演示,可以通过触摸持续时间在节点上应用脉冲。方法很简单:触摸开始时设置一个BOOL变量YES
,触摸结束时设置NO
。触摸时,它会在update
方法中施加恒定的脉冲。
为了让游戏更自然,您可能需要细化冲动动作,或者随着节点的上升而向下滚动背景。
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
您好, 我是 spriteKit 的新手,我正在尝试制作游戏。在游戏中,我有一个从一个楼梯跳到另一个楼梯的玩家,它从屏幕的顶部无限跳跃(就像在涂鸦跳跃中一样,只有跳跃是由玩家的触摸控制的)。我试图通过对玩家施加冲动来跳跃,但我想通过玩家的触摸持续时间来控制跳跃强度。我该怎么做?当玩家 STARTS 触摸屏幕时执行跳跃,所以我无法测量跳跃强度(通过计算触摸持续时间)...有什么想法吗? 提前致谢!!! (:
这里有一个简单的演示,可以通过触摸持续时间在节点上应用脉冲。方法很简单:触摸开始时设置一个BOOL变量YES
,触摸结束时设置NO
。触摸时,它会在update
方法中施加恒定的脉冲。
为了让游戏更自然,您可能需要细化冲动动作,或者随着节点的上升而向下滚动背景。
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