gpt4 book ai didi

ios - 根据 Sprite 的当前位置让 Sprite 移动到某个位置?

转载 作者:行者123 更新时间:2023-11-29 02:44:19 25 4
gpt4 key购买 nike

我认为我的标题相当困惑,但我不确定如何命名。这是我当前的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
SKAction *moveNodeRight = [SKAction moveByX:-209.0 y:0.0 duration:0.85];
[_squirrelSprite runAction: moveNodeRight withKey:@"changeside"];
//_squirrelSprite.xScale = -1.0;

if(_squirrelSprite.xScale == -1.0)
{
_squirrelSprite.xScale = 1.0;
} else {
_squirrelSprite.xScale = -1.0;
}
}

当我点击屏幕时, Sprite 移动到一个新位置 (X:-209.0, Y:0.0)。新位置在当前位置的左侧。当我第二次点击屏幕时, Sprite 只是移动到离屏幕更远的左边。

我想要的:我对目前所拥有的感到满意,但是当 Sprite 处于新位置(X:-209.0,Y:0.0)时,我希望第二次点击将其返回到原始位置.我认为它看起来像上面的代码,但到目前为止没有任何效果。下面是它可能是什么样子的想法。谢谢!

if(_squirrelSprite.position == the second position)
{
_squirrelSprite.position = the first position;
} else {
_squirrelSprite.xScale = the second position;
}

最佳答案

当用户点击屏幕时,这会将节点从一个位置移动到另一个位置。然后,当用户再次点击屏幕时,它会将节点移回原始位置。

@interface MyScene()

@property SKSpriteNode *squirrelSprite;
@property BOOL atFirstPosition;
@property CGPoint firstPosition;

@end

@implementation MyScene

-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
_firstPosition = CGPointMake(300, self.frame.size.height/2);
_squirrelSprite = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(32, 32)];
_squirrelSprite.position = _firstPosition;
_atFirstPosition = YES;
[self addChild:_squirrelSprite];
}
return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (_atFirstPosition)
{
SKAction *moveNodeLeft = [SKAction moveByX:-209.0 y:0.0 duration:0.85];
[_squirrelSprite runAction: moveNodeLeft withKey:@"changeside"];
} else {
_squirrelSprite.position = _firstPosition;
}
_atFirstPosition = !_atFirstPosition;
_squirrelSprite.xScale *= -1.0;
}

@end

编辑:这将使用 SKAction 将节点移回其原始位置

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (_atFirstPosition)
{
SKAction *moveNodeLeft = [SKAction moveByX:-209.0 y:0.0 duration:0.85];
[_squirrelSprite runAction: moveNodeLeft withKey:@"moveleft"];
} else {
SKAction *moveNodeRight = [SKAction moveByX:209.0 y:0.0 duration:0.85];
[_squirrelSprite runAction: moveNodeRight withKey:@"moveright"];
}
_atFirstPosition = !_atFirstPosition;
_squirrelSprite.xScale *= -1.0;
}

关于ios - 根据 Sprite 的当前位置让 Sprite 移动到某个位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25337151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com