[Resolved] Object Intherance

module (..., package.seeall);  
  
function new()  
 local mosca = display.newImage("media/mosca.png");  
 mosca.x = 0; mosca.y = 100;  
 mosca.xScale = 0.2; mosca.yScale = 0.2;  
  
 local remove\_monster = function( obj )  
 obj:removeSelf();  
 end  
  
 function touch(e)  
 if (e.phase == "ended") then  
 -- mosca.isVisible = false;  
 transition.to( e.target, { time=1, alpha=0, onComplete=remove\_monster } )  
  
 end  
 end  
  
 function animate(e)  
 local rand\_x = math.random(20,580);  
 local rand\_y = math.random(20,300);  
 --mosca.x = rand\_x;  
 transition.to( mosca { time=500, x=120 } )  
 end  
  
 Runtime:addEventListener("enterFrame", animate);  
 mosca:addEventListener( "touch", touch );  
  
 return mosca;  
  
end  

Hi everybody,
I have this code… all it’s ok, but when i call the

function animate(e)  
 local rand\_x = math.random(20,580);  
 local rand\_y = math.random(20,300);  
 --mosca.x = rand\_x;  
 transition.to( mosca { time=500, x=120 } )  
 end  

in details the function

transition.to( mosca { time=500, x=120 } )  

i cannot access to the mosca variable… Someone can help me?
What i need is to animate various enemy in a screen…

I post the main.lua source too…

local mosca = require("mosca");  
  
local enemy = {}  
-- populate enemy array and dispose enemy  
for a = 1, 2 do   
 local rand\_x = math.random(0,580);  
 local rand\_y = math.random(0,300);  
 enemy[a] = mosca.new(); -- create new instance  
  
 -- positioning  
 --if a \>= 2 then  
 -- enemy[a].move( (a\*90) )  
 --end -- end positioning  
 -- Runtime:addEventListener( "animate", enemy[a].animate );  
end -- end for  
local function someFunction()  
 for i=1,#enemy do  
 local rand\_x = math.random(20,580);  
 local rand\_y = math.random(20,300);  
  
  
 if (enemy[i].x \< rand\_x) then  
 enemy[i].xScale = -0.2   
 else  
 enemy[i].xScale = 0.2   
 end  
 transition.to( enemy[i], { time=1000, transition = easing.inQuad, x=rand\_x, y=rand\_y } )   
 end  
 transition.to( enemy[#enemy], { time=500, x=rand\_x, y=rand\_y, onComplete=someFunction } )  
end  
  
--someFunction()  
  

P.s. in this code i’m using the someFunction for animate the object, but i DON’T WANT to use this function in the main.lua… I like to have the animation code in the mosca.lua file!!

thanks.
This is the error:
[C]: in function ‘mosca’
…o\documents\corona projects\mosca-cojonera\mosca.lua:24: in function
<…o error> …o\documents\corona projects\mosca-cojonera\mosca.lua:24: attempt to c
all upvalue ‘mosca’ (a table value)
stack traceback:
[C]: in function ‘mosca’
…o\documents\corona projects\mosca-cojonera\mosca.lua:24: in function
<…o error> …o\documents\corona projects\mosca-cojonera\mosca.lua:24: attempt to c
all upvalue ‘mosca’ (a table value)
stack traceback:
[C]: in function ‘mosca’
…o\documents\corona projects\mosca-cojonera\mosca.lua:24: in function
<…o topic_id: reply_id:></…o></…o></…o>

Missing comma:

[lua]transition.to( mosca, { time=500, x=120 } )[/lua] [import]uid: 8271 topic_id: 26967 reply_id: 109463[/import]

function animate(e)  
 local rand\_x = math.random(20,580);  
 local rand\_y = math.random(20,300);  
 --mosca.x = rand\_x;  
 --print(e)  
 transition.to( mosca, { time=500, x=rand\_x, y=rand\_y } )  
 --mosca:translate( rand\_x, rand\_y)  
 end  
  
 mosca:addEventListener( "touch", touch );  
 Runtime:addEventListener("enterFrame", animate);  

I have put the comma, but the code don’t work… My image is freeze…

and if i uncomment the --mosca.x = rand.x, the image move…

Why with the transition.to the image don’t move? [import]uid: 153233 topic_id: 26967 reply_id: 109471[/import]

You’ve changed your code - now you’re calling the animate function on every render of the frame, that’s 30 or 60 times a second. The existing transition will get cancelled each time you create a new transition.

Try this:

[lua]function animate(e)
local rand_x = math.random(20,580);
local rand_y = math.random(20,300);
–mosca.x = rand_x;
–print(e)
transition.to( mosca, { time=500, x=rand_x, y=rand_y } )
–mosca:translate( rand_x, rand_y)
end

– mosca:addEventListener( “touch”, touch );
animate()[/lua]

[import]uid: 8271 topic_id: 26967 reply_id: 109500[/import]

Thank you so much :slight_smile:

I solved the problem with this “trick”

function animate(e)  
 local rand\_x = math.random(20,580);  
 local rand\_y = math.random(20,300);  
 --mosca.x = rand\_x;  
 --print(e)  
 transition.to( mosca, { time=500, x=rand\_x, y=rand\_y, onComplete=animate } )  
 --mosca:translate( rand\_x, rand\_y)  
 end  
  
-- mosca:addEventListener( "touch", touch );  
end  
  
animate()   
  

In this way when the transition.to end, i will start a new function animate :slight_smile: [import]uid: 153233 topic_id: 26967 reply_id: 109693[/import]