Some help for a variable

Hi corona community :slight_smile:

i need some help with this code, it is very important because i have this doubt very times, and need to understand it now :slight_smile:

i have a block and a circle, and when i rotate the circle, the object will automatically rotate too, but i need to catch the variable t.rotation, that is inside the local function rotateOBJ, i need this variable value to do this: block1.rotation = t.rotation , outside the rotationOBJ function, how i can do it?
if anything that you dont understand, tell me

thanks very much

[code]

local circle = display.newImage(“rodar.png”)
circle.x = 100;
circle.y=100;

local function rotateObj(event)
local t = event.target
local phase = event.phase
if (phase == “began”) then

–…some code

elseif t.isFocus then
if (phase == “moved”) then

–…some code
rotationAmt = angle1 - angle2
t.rotation = t.rotation - rotationAmt
–t.rotation=value
print ("t.rotation = "…t.rotation)
t.x1 = t.x2
t.y1 = t.y2

elseif (phase == “ended”) then
–…some code

end

end

return true

end

circle:addEventListener(“touch”, rotateObj)

local block1 = display.newImage(“block1_img.png”)
block1.rotation = t.rotation
block1.x = 40;
block1.y=50;

[/code] [import]uid: 26056 topic_id: 17727 reply_id: 317727[/import]

If I’m reading your code correctly, you have t declared as a local variable to the rotateObj so you can’t use that outside of that function.

Couple of options from here…

  1. If you want the block to rotate as the circle does, then I would move your block declaration up above the rotateObj function and just set the block.rotation inside the function to the same as the circle

2)If you want to change the block rotation later at some point, you could just set the block rotation to match the circle rotation. (IE block1.rotation = circle.rotation)

3)You could declare another variable up top like the circle to hold the rotation value from inside rotateObj function to be used outside, later on in your code, but this is not really needed if you have scope to the circle itself.
Hope that helps…

Croisened

[import]uid: 48203 topic_id: 17727 reply_id: 67614[/import]

thank you very much croisened, your help is great :slight_smile:
[import]uid: 26056 topic_id: 17727 reply_id: 67624[/import]

now if i rotate the roda “roda is a circle” the block rotation is equal, but one thing is wrong, i have 2 blocks , block1 and block2, and have 2 circles, circle1 and circle2, i need to when i rotate the circle1 it will change the rotation of only block1, and when i rotate the circle2 it will rotate only the block2, and i cant do it, because when i rotate the circle1 or circle2 it will rotate both of blocks equal, i can resolve this problem using 2 equal functions, one to block1 and circle 1 and other to block2 and circle2 but it isnt good because i have a lot of objects and will not a good programming method. i need to use the same function to a lot of objects.
the problem is this part:
roda1.rotation = t.rotation
block1.rotation = roda1.rotation
roda2.rotation = t.rotation
block2.rotation = roda2.rotation

because when i use the cirlcle1 or circle2 he do the same rotation for both of blocks

[code]
local roda1 = display.newImage(“rodar.png”)
local block1 = display.newImage(“block1_img.png”)

local roda2 = display.newImage(“rodar.png”)
local block2 = display.newImage(“block1_img.png”)

block1.x = 200;
block1.y=340;
block2.x = 100;
block2.y=200;

roda1.x = 200;
roda1.y= 200;
roda2.x = 100;
roda2.y= 100;

local function rotateObj(event)
local t = event.target
local phase = event.phase
if (phase == “began”) then
–some code

elseif t.isFocus then
if (phase == “moved”) then

–some code
rotationAmt = angle1 - angle2

t.rotation = t.rotation - rotationAmt

roda1.rotation = t.rotation
block1.rotation = roda1.rotation
roda2.rotation = t.rotation
block2.rotation = roda2.rotation

–some code

elseif (phase == “ended”) then
– some code
end

end
– Stop further propagation of touch event
return true

end

roda1:addEventListener(“touch”, rotateObj)
roda2:addEventListener(“touch”, rotateObj)
[/code] [import]uid: 26056 topic_id: 17727 reply_id: 67939[/import]