I need help with Tweens. What I want to do is when I swipe right it changes the x pos of a sprite by 160 and when I swipe left it changes the sprites x by -160. But when I swipe an error says “subject must be a table or user data.” I’ve looked at the “Sprites and Tweens” discussion and tried it out but nothing changed. Can someone please help?
Here is the parameters tween takes: tween(time, table holding object(s) you want to change, table including new values for those you want to change, tween type, callback)
<br \>
So, if the x is a global variable, you need to do something like this tween(1, _G, { x = x + 160 }, tween.easing.linear)
<br \>
Explaining the parameters:
- Time: I chose a random 1 second, you can change it to your liking
- Table 1: `_G` is the table in which global variables are stored
- Table 2: This table includes an x value which is the current x value + 160, so it will change x to be 160 more
- Tween Type: I chose the default tween type, you can pick a different one if you want a slightly different effect
- Callback: I didn't put a callback, but you can add one if you need it for something else.
<br \>
Hope this helps!
I just don’t get the _g part! is that what I would put or what?
Sorry, that wasn’t supposed to be an explanation point, it was supposed to be a coma
Also, the x variable is Pcar.x (Player car)
_G is just a table holding all the global variables.
So you car could be a table like:
Pcar={x=WIDTH/2,y=HEIGHT/2}
If (swipe right) then
tween(1,Pcar,{x=x+160})
end
this will change Pcar.x from its current value to it plus 160 pixels, in 1s.
tween(1, Pcar, { x = Pcar.x + 160 })
@Coder are you sure about {x=x+160})
? The second x
here is a global variable named x, and not a field of Pcar. This is why i wrote { x = Pcar.x + 160 }
@Jmv38, you are correct. I think @Coder was thinking about global vars.
@CrazyJoe73, I explained _G in my first post, but since you are using Pcar.x, Jmv is correct.
Dang it I don’t know why but none of this is working. I’ll just give some code and maybe it will help. This is the sprite and tween code:
sprite(self.costume,pcarx,self.y,self.w,self.h)
tween(2, pcarx,{pcarx == Pcar.x + 160},tween.easing.quadInOut)
I made Pcar.x equal to pcarx unless it is moving right or left and then is equal to it again after it stops
I still get this:
error: [string “-- tween.lua …”]:98: subject must be a table or userdata. Was 592
Or when I make the object Pcar instead of pcarx I get this:
error: [string “-- tween.lua …”]:54: Parameter ‘1’ must be a number or table of numbers
outch! So Many may errors just in 2 lines of code. Impossible to help you properly Ith just those 2 lines.Post all the code if you want some help.
And put the code between blocs of ~~~ to keep it in blue.
One of the problems is thatpcarx
is a variable not a table. Tweens require tables so:
pcarPos={x=0,y=0}
tween(2,pcarPos,{x=pcarPos.x+160},tween.easing.quadInOut)
It finally worked! I just tweeted up coder’s code and did the _G as the object. Thanks for your help all of you!