你好,我是spriteKit的新手,我正试着做一个游戏。在游戏中,我有一个从楼梯跳到楼梯的玩家,它无限地从屏幕顶部跳出来(就像在涂鸦跳跃中,只有跳跃是由玩家的触摸控制的)。我试图通过对玩家施加一种冲动来进行跳跃,但是我想通过玩家的触碰持续时间来控制跳跃的强度。我怎样才能做到呢?当玩家启动触摸屏幕时,跳跃执行,所以我无法测量跳强度(通过计算触摸持续时间).有什么想法吗?预先谢谢! (:
发布于 2015-09-12 07:56:17
这里有一个简单的演示,可以将脉冲应用到具有触摸时间的节点上。这个方法很简单:在触摸开始时设置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
https://stackoverflow.com/questions/32540312
复制