简体   繁体   English

从SKAction(SpriteKit)制作属性时遇到问题

[英]Problems making a property from a SKAction (SpriteKit)

Again, probably a newbie question. 同样,可能是一个新手问题。 I'm following the instructions I found in these two links, since they solve exactly the problem I have (modify the speed of a SKAction from outside its own method): 我按照在这两个链接中找到的说明进行操作,因为它们可以完全解决我遇到的问题(可以从其自己的方法之外修改SKAction的速度):

How to change duration of executed SpriteKit action 如何更改SpriteKit操作的执行时间

How to run or execute an SKAction from outside of the object? 如何从对象外部运行或执行SKAction?

In my case, have this SKAction: 就我而言,请使用以下SKAction:

SKAction * moveBall = [SKAction moveToY:0 duration:1];

[ball runAction:moveBall withKey:@"ball falling"]; 

I create the property, like this: 我创建属性,如下所示:

@property SKAction * moveBall;

And then I want to call it from the touchesBegan after touching a button, like this: 然后,我想从touchesBegan调用按钮后调用它,像这样:

if ([node.name isEqualToString:@"Slow Down Button"]) {

        self.moveBall.speed = 0.5;

    }

moveBall.speed inside its own method is 1.0, but self.moveBall.speed indicates a speed of 0.0 (same for _moveBall.speed ), so the property declaration is not working correctly. moveBall.speed其自己的方法里面是1.0,但self.moveBall.speed表示0.0的速度(同样为_moveBall.speed ),所以在属性声明不能正常工作。 I tried several things, but so far I couldn't find what is missing. 我尝试了几件事,但到目前为止我找不到丢失的内容。

Thanks in advance! 提前致谢!

This: 这个:

SKAction * moveBall = [SKAction moveToY:0 duration:1];

creates a local variable named moveBall . 创建一个名为moveBall的局部变量。 But your property is accessed through self.moveBall respetively directly through its auto-synthesized ivar named _moveBall . 但是,可以通过self.moveBall直接通过其自动合成的ivar名为_moveBallself.moveBall访问您的媒体_moveBall

The solution is one of these two: 解决方案是以下两个之一:

self.moveBall = [SKAction moveToY:0 duration:1];

or 要么

_moveBall = [SKAction moveToY:0 duration:1];

and then continue to use that variable for addChild: and other uses. 然后继续将该变量用于addChild:和其他用途。

You most likely need to spend some time reading about properties and pointers. 您很可能需要花一些时间来阅读有关属性和指针的信息。 But it's possible that you just aren't showing all the code you are using. 但是有可能您没有显示正在使用的所有代码。 The phrase "inside its own method" is confused. 短语“在其自己的方法内”是困惑的。

SKAction *moveBall = is not the same as _moveBall, unless you have assigned that pointer to the property. SKAction * moveBall =与_moveBall不同,除非您已将该指针分配给属性。 But you are using the same name for a pointer and a property in your code, confusing things further. 但是,您在代码中使用了相同的名称作为指针和属性,这进一步混淆了事情。

I suspect you need to change: 我怀疑您需要更改:

SKAction * moveBall = [SKAction moveToY:0 duration:1];

to

_moveBall = [SKAction moveToY:0 duration:1];

and then only use _moveBall throughout your code. 然后仅在整个代码中使用_moveBall。 But make sure you read up on pointers and properties and understand the difference. 但是请确保您阅读了指针和属性并理解​​了它们之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM