我正在尝试为我的游戏加水。除了不同的背景色外,其他内容不多。
但是,我希望player-sprite
float 在它的顶部(或中间)。如果玩家只是从下面走进水中,我希望他漂浮到顶部。如果他跌倒了,我希望他慢慢改变方向,然后漂浮起来。
当他在水中时,我曾尝试使重力为负,但这给了我一些不想要的效果。例如,当他(玩家)浮出水面时,法向重力会将他推回向下,水将其推向上方,依此类推。最终,玩家将被从一端推向另一端,在水中“弹跳”。当他浮出水面时,我希望他从容地呆在水面。我怎样才能做到这一点?
这是我update-loop
中的代码:
SKNode *backgroundNodeAtPoint = [_bgLayer nodeAtPoint:_ball.position];
if ([backgroundNodeAtPoint.name isEqualToString:@"WATER"]) {
self.physicsWorld.gravity = CGVectorMake(self.physicsWorld.gravity.dx, 2);
} else {
if (self.physicsWorld.gravity.dy != -4) {
self.physicsWorld.gravity = CGVectorMake(self.physicsWorld.gravity.dx, -4);
}
}
基本上,当玩家在水中时,这会将我的重力更改为
2
,否则将其更改为-4
,除非它已经是-4
。谢谢!
最佳答案
我相信您在模拟水方面有三种可能的选择。
1)如评论中所述,您可以尝试使用SKFieldNode(iOS 8+)。但是从我个人的经验来看,现场节点并没有真正为我做这件事,因为除非对其进行大量定制,否则您将无法对其进行太多控制,在这种情况下,您最好只是从头开始自己的计算并减少复杂。
2)您可以在水中时调整 Sprite 的线性和旋转阻尼。实际上,甚至苹果公司在其文档中的引用中都提到了这一点。但是,这不会给您带来浮力。
3)自己执行计算。在更新方法中,检查 body 何时进入您的“水”中,何时可以计算出粘度和/或浮力,并相应地调整节点的速度。我认为这是最佳选择,但也更加困难。
编辑:我只是在Swift中编写了选项3的快速示例。我认为这就是您要寻找的。我在顶部添加了因子常量,因此您可以对其进行调整以准确获得所需的内容。动态地应用 Action ,因此不会干扰您当前的速度(即,您可以在水中控制角色)。下面是场景和gif的代码。请记住,增量时间假定为每秒60帧(1/60),并且没有速度钳制。这些是您可能想要或不想要的功能,具体取决于您的游戏。
迅捷
class GameScene: SKScene {
//MARK: Factors
let VISCOSITY: CGFloat = 6 //Increase to make the water "thicker/stickier," creating more friction.
let BUOYANCY: CGFloat = 0.4 //Slightly increase to make the object "float up faster," more buoyant.
let OFFSET: CGFloat = 70 //Increase to make the object float to the surface higher.
//MARK: -
var object: SKSpriteNode!
var water: SKSpriteNode!
override func didMoveToView(view: SKView) {
object = SKSpriteNode(color: UIColor.whiteColor(), size: CGSize(width: 25, height: 50))
object.physicsBody = SKPhysicsBody(rectangleOfSize: object.size)
object.position = CGPoint(x: self.size.width/2.0, y: self.size.height-50)
self.addChild(object)
water = SKSpriteNode(color: UIColor.cyanColor(), size: CGSize(width: self.size.width, height: 300))
water.position = CGPoint(x: self.size.width/2.0, y: water.size.height/2.0)
water.alpha = 0.5
self.addChild(water)
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
}
override func update(currentTime: CFTimeInterval) {
if water.frame.contains(CGPoint(x:object.position.x, y:object.position.y-object.size.height/2.0)) {
let rate: CGFloat = 0.01; //Controls rate of applied motion. You shouldn't really need to touch this.
let disp = (((water.position.y+OFFSET)+water.size.height/2.0)-((object.position.y)-object.size.height/2.0)) * BUOYANCY
let targetPos = CGPoint(x: object.position.x, y: object.position.y+disp)
let targetVel = CGPoint(x: (targetPos.x-object.position.x)/(1.0/60.0), y: (targetPos.y-object.position.y)/(1.0/60.0))
let relVel: CGVector = CGVector(dx:targetVel.x-object.physicsBody.velocity.dx*VISCOSITY, dy:targetVel.y-object.physicsBody.velocity.dy*VISCOSITY);
object.physicsBody.velocity=CGVector(dx:object.physicsBody.velocity.dx+relVel.dx*rate, dy:object.physicsBody.velocity.dy+relVel.dy*rate);
}
}
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {object.position = (touches.anyObject() as UITouch).locationInNode(self);object.physicsBody.velocity = CGVectorMake(0, 0)}
}
Objective-C
#import "GameScene.h"
#define VISCOSITY 6.0 //Increase to make the water "thicker/stickier," creating more friction.
#define BUOYANCY 0.4 //Slightly increase to make the object "float up faster," more buoyant.
#define OFFSET 70.0 //Increase to make the object float to the surface higher.
@interface GameScene ()
@property (nonatomic, strong) SKSpriteNode* object;
@property (nonatomic, strong) SKSpriteNode* water;
@end
@implementation GameScene
-(void)didMoveToView:(SKView *)view {
_object = [[SKSpriteNode alloc] initWithColor:[UIColor whiteColor] size:CGSizeMake(25, 50)];
self.object.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.object.size];
self.object.position = CGPointMake(self.size.width/2.0, self.size.height-50);
[self addChild:self.object];
_water = [[SKSpriteNode alloc] initWithColor:[UIColor cyanColor] size:CGSizeMake(self.size.width, 300)];
self.water.position = CGPointMake(self.size.width/2.0, self.water.size.height/2.0);
self.water.alpha = 0.5;
[self addChild:self.water];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
}
-(void)update:(NSTimeInterval)currentTime {
if (CGRectContainsPoint(self.water.frame, CGPointMake(self.object.position.x,self.object.position.y-self.object.size.height/2.0))) {
const CGFloat rate = 0.01; //Controls rate of applied motion. You shouldn't really need to touch this.
const CGFloat disp = (((self.water.position.y+OFFSET)+self.water.size.height/2.0)-((self.object.position.y)-self.object.size.height/2.0)) * BUOYANCY;
const CGPoint targetPos = CGPointMake(self.object.position.x, self.object.position.y+disp);
const CGPoint targetVel = CGPointMake((targetPos.x-self.object.position.x)/(1.0/60.0), (targetPos.y-self.object.position.y)/(1.0/60.0));
const CGVector relVel = CGVectorMake(targetVel.x-self.object.physicsBody.velocity.dx*VISCOSITY, targetVel.y-self.object.physicsBody.velocity.dy*VISCOSITY);
self.object.physicsBody.velocity=CGVectorMake(self.object.physicsBody.velocity.dx+relVel.dx*rate, self.object.physicsBody.velocity.dy+relVel.dy*rate);
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
self.object.position = [(UITouch*)[touches anyObject] locationInNode:self];
self.object.physicsBody.velocity = CGVectorMake(0, 0);
}
@end
关于ios - 在Spritekit中模拟水/在水上制作Sprite “float”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25344808/