How to make a thing rotate

In the last post i asked a question for How to make things rotate with touch …Thanx for your good answer and that is 

local function rotateBox(event) if (event.phase=="began) then box.rotation = box.rotation+90 end end Runtime:addEventListener("touch , rotateBOx)

But know i want to say that for suppose we have 3 boxes and i only want to rotate the box which i touch and not the rest one till i touch them…sorry for silly question :stuck_out_tongue: but hoping for replying soon and please give the easiest one like this above… 

Thanx in advance :slight_smile:

local function rotateBox(event)     if event.phase == "began" then         event.target.rotation = event.target.rotation + 90     end       return true end   box:addEventListener("touch", rotateBox)

What this does:

  1. You add an event listener to the display object, rather than the runtime. This means that each display object you attach a listener to can be accessed via the same function (if required)

  2. event.target: this is the object you have touched. So say you add the same listener to 4 objects, you can identify the one you interacted with via event.target

  3. return true: this means that the touch event won’t leak down to objects below the object you touched. 

Hope this helps

Ya bro its cool but say that i have three crates… crate A, crateB and crateC…and if i want to rotate crate A then how should i do that…as you directly pointing towards function but it more understandable if you include crate names also…Thank you, you are awesome :slight_smile:

local function rotateBox(event)     if event.phase == "began" then         event.target.rotation = event.target.rotation + 90     end     return true end box:addEventListener("touch", rotateBox) -- change this -- ex: crateA:addEventListener("touch", rotateBox) -- Add one to crate b (if required) crateB:addEventListener("touch", rotateBox)

As stated in #1 in my previous post, you prefix the addEventListener call with the object that you want to listen/receive touch events for.

I’ve edited the example above to show you how to add listeners to more than one object.

Just note that if you add the event listener to 3 objects (as in the example) then all the objects will rotate upon touch.

You should always reference the object in the function by event.target rather than its variable name (where appropriate), as it is good programming practise, and avoids some pitfalls. In this case, event.target is the object you attached the listener to via addEventListener()

To get around this, you can either:

A) Add different functions for each crate

B ) Give each object a .name property so you can check the objects name in the touch handler, to give it specific actions upon touch.

Here is an example of option B

local function rotateBox(event)     if event.phase == "began" then         if event.target.name == "crateA" then             event.target.rotation = event.target.rotation + 90         end     end     return true end box:addEventListener("touch", rotateBox) -- change this -- ex: crateA.name = "crateA" crateA:addEventListener("touch", rotateBox) -- Add one to crate b (if required) crateB.name = "crateB" crateB:addEventListener("touch", rotateBox)

Hope this helps

PS: I strongly recommend you read through this free book: https://www.lua.org/pil/contents.html#P1

It is written by the creator of Lua, and will teach you a lot about the programming language.

But what does that

return true

means?

I stated in my previous replies:

  1. return true: this means that the touch event won’t leak down to objects below the object you touched.

So basically… imagine you had two crates on top of each other (crateA, crateB). If you don’t add:

return true

Then you will receive a touch event for both crateA and crateB (in the first example I gave, both crates would rotate). With it, you will only receive an event for the crate you touched, and nothing below it (only the first crate you touched would rotate).

Thanx you are genious and awesome :slight_smile:

I don’t know about that first part :wink:

We all started somewhere, so no problem!

Just be sure to read the book I linked to. I believe it may also be available in other languages if you do a google search.

Best of luck!

local function rotateBox(event)     if event.phase == "began" then         event.target.rotation = event.target.rotation + 90     end       return true end   box:addEventListener("touch", rotateBox)

What this does:

  1. You add an event listener to the display object, rather than the runtime. This means that each display object you attach a listener to can be accessed via the same function (if required)

  2. event.target: this is the object you have touched. So say you add the same listener to 4 objects, you can identify the one you interacted with via event.target

  3. return true: this means that the touch event won’t leak down to objects below the object you touched. 

Hope this helps

Ya bro its cool but say that i have three crates… crate A, crateB and crateC…and if i want to rotate crate A then how should i do that…as you directly pointing towards function but it more understandable if you include crate names also…Thank you, you are awesome :slight_smile:

local function rotateBox(event)     if event.phase == "began" then         event.target.rotation = event.target.rotation + 90     end     return true end box:addEventListener("touch", rotateBox) -- change this -- ex: crateA:addEventListener("touch", rotateBox) -- Add one to crate b (if required) crateB:addEventListener("touch", rotateBox)

As stated in #1 in my previous post, you prefix the addEventListener call with the object that you want to listen/receive touch events for.

I’ve edited the example above to show you how to add listeners to more than one object.

Just note that if you add the event listener to 3 objects (as in the example) then all the objects will rotate upon touch.

You should always reference the object in the function by event.target rather than its variable name (where appropriate), as it is good programming practise, and avoids some pitfalls. In this case, event.target is the object you attached the listener to via addEventListener()

To get around this, you can either:

A) Add different functions for each crate

B ) Give each object a .name property so you can check the objects name in the touch handler, to give it specific actions upon touch.

Here is an example of option B

local function rotateBox(event)     if event.phase == "began" then         if event.target.name == "crateA" then             event.target.rotation = event.target.rotation + 90         end     end     return true end box:addEventListener("touch", rotateBox) -- change this -- ex: crateA.name = "crateA" crateA:addEventListener("touch", rotateBox) -- Add one to crate b (if required) crateB.name = "crateB" crateB:addEventListener("touch", rotateBox)

Hope this helps

PS: I strongly recommend you read through this free book: https://www.lua.org/pil/contents.html#P1

It is written by the creator of Lua, and will teach you a lot about the programming language.

But what does that

return true

means?

I stated in my previous replies:

  1. return true: this means that the touch event won’t leak down to objects below the object you touched.

So basically… imagine you had two crates on top of each other (crateA, crateB). If you don’t add:

return true

Then you will receive a touch event for both crateA and crateB (in the first example I gave, both crates would rotate). With it, you will only receive an event for the crate you touched, and nothing below it (only the first crate you touched would rotate).

Thanx you are genious and awesome :slight_smile:

I don’t know about that first part :wink:

We all started somewhere, so no problem!

Just be sure to read the book I linked to. I believe it may also be available in other languages if you do a google search.

Best of luck!