How to move an object randomly with transition.to? - lua

I'm trying to move an object randomly to different locations, so I came out with the following: transition.to generates the x,y randomly as well as the time, and on finish runs another function which checks if the object is still there and sends it to a different location.
but I'm getting an error:
Runtime error
main.lua:352: stack overflow
stack traceback:
main.lua:352: in function
'toAnotherPlace'
looks like corona doesn't really waits for transition complete, so it goes on infinite loop
code
function toAnotherPlace(object)
if object ~= nil then
transition.to( object,
{
time=math.random(1500,6000),
alpha=1,
x=(math.random(10, 310)),
y=(math.random(10, 400)),
onComplete=toAnotherPlace(object)
})
end
end
transition.to( bossess[boss],
{
time=math.random(1500,6000),
alpha=1,
x=(math.random(10, 310)),
y=(math.random(10, 400)),
onComplete=toAnotherPlace(bossess[boss])
})

You can try this, I added an onComplete = function() ... end and I call the toAnotherPlace(object) function inside it.
I think it's a bug if you directly call a function on onComplete
function toAnotherPlace(object)
print(object.width)
if object ~= nil then
transition.to( object,
{
time = math.random(1500,6000),
alpha = 1,
x = math.random(10, 310),
y = math.random(10, 400),
onComplete = function()
toAnotherPlace(object)
end
})
end
end
transition.to(bossess[boss],
{
time = math.random(1500,6000),
alpha = 1,
x = math.random(10, 310),
y = math.random(10, 400),
onComplete = function()
toAnotherPlace(bossess[boss])
end
})
I tried this and is working fine, no errors.
If you still getting errors, check the bossess[boss] if there is a reference to your object

Related

Modify the time value of a transition

How can i change the time value of a transition while is executing? For example I have the object "cesta" moving from the left to the right with a time of 4000ms then for some reason I want to change the time value to move it faster.
function createCesta()
...
transition.to(cesta, {time = 4000, x = screenW + 110})
...
end
function touchScreen(event)
if event.phase == "began" then
end
if event.phase == "ended" then
--change the time value from here "from 4000 to 2000"
end
end
The docs at http://docs.coronalabs.com/api/library/transition/index.html indicate that there is no function call to do this. You would therefore have to cancel the current incomplete transition and create a new one. For example,
local trans
local transTime = 4000 -- ms
local transStart
local object
function someEventHandler(event)
transition.cancel(trans)
local remaining = system.getTimer() - transStart - transTime
if remaining > 0 then
trans = transition.to(object, { time = remaining/2, x = ... }
end
end
function spawn()
object = display.newText(...)
trans = transition.to(object, {time = transTime}
transStart = system.getTimer()
end
This shows a spawn function where you create a display object and make it move via transition to some x, and an event handler that will get called at some point. It computes how much time is left in the transition and if > 0, creates a new transition with half that remaining time so double the "motion" speed of x.

error loading module '(' expected near '='

This code is to create function listeners for the cannon shooting. When i run the code, it gave me an error Question1.lua:43 '('expected near '='
function cannonCharge = function(event)
if(event.phase == 'began') then
impulse = 0
cannon.isVisible = true
Runtime:addEventListener('enterFrame', charge)
print ('cannonCharge')
end
end
function shot = function(event)
if(event.phase == 'ended') then
Runtime:removeEventListener('enterFrame', charge)
cannon.isVisible = true
cannon.rotation = 0
cannonBall = display.newImage('cannon ball.png', 84, 220)
physics.addBody(cannonBall, {density = 1, friction = 0, bounce = 0})
cannonBalls:insert(cannonBall)
print ('shot')
-- Shoot cannon ball
cannonBall:applyLinearImpulse(3, impulse, cannonBall.x, cannonBall.y )
--Collision listener
cannonBall:addEventListener ('collision', ballCollision)
end
end
function scene:createScene(event)
...
I added the listeners to enterScene
function scene:enterScene( event )
local group = self.view
background:addEventListener('touch', cannonCharge)
background:addEventListener('touch', shot)
end
Variables don't have types; Only values do. So, instead of
function shot = function(event)
-- ...
end
Try
local shot = function(event)
-- ...
end
If you don't put local, the variable will be global. Use of globals should be minimized.
If you prefer a more structured syntax, you can use:
local function shot(event)
-- ...
end
That's equivalent to:
local shot
shot = function(event)
-- ...
end
You can not assign two touch listener on the same object. Because it's create conflict to which function it's call first.
instead of that, you need to assign one touch and one tap listener. So there is no conflict.
background:addEventListener('tap', cannonCharge)
background:addEventListener('touch', shot)

Lua stack overflow in recursive function in corona sdk

For some reason the code below throws a stack overflow error if the else statement gets executed too many times. I am trying to have the scene.targeting function select a target from the objTable passed in the params, but only targets with a .tgtFlag == false are valid selections. If the function selects a target that has a .tgtFlag == true, it recalls the scene.targeting function passing in the same set of params.
The line that breaks is local theTarget = params.objTable[math.random( 1, #params.objTable )] but only after else scene.targeting(params) end is called several times.
Any help would be greatly appreciated.
function scene.targeting( params ) -- Targeting functions
function animateTarget( target )
if target.savedFlag == false then
transition.to( target, {time = 100, y = target.y - 15} )
transition.to( target, {time = 100, delay = 150, y = target.y, onComplete = animateTarget} )
end
end
local theTarget = params.objTable[math.random( 1, #params.objTable )]
if theTarget.tgtFlag == false then
theTarget.tgtFlag = true
animateTarget(theTarget)
else
scene.targeting(params)
end
end
Referring to Programming in Lua:
A tail call is a kind of goto dressed as a call. A tail call happens when a function calls another as its last action, so it has nothing else to do.
In your example, animateTarget obviously isn't called in such a way, and can and will suffer from stack overflows. Either rewrite it to make use of TCO or change it to non-recursive version.

Corona SDK 'nil value' issue

I am making a shooter game using the corona sdk...
My problem is that i am trying to delete an object(that is part of an array)when it leaves the screen.. when i do, i get an error that says 'trying to compare nil value to variable' which refers to a simple move function for every object in the array. Here is relevant parts of code:
function addAlien()
listeners('add')
end
function listeners(action)
if(action == 'add') then
Runtime:addEventListener('enterFrame',update)
enemyTimer = timer.performWithDelay(800,addEnemy,0)
else
Runtime:removeEventListener('enterFrame',update)
timer.cancel(enemyTimer)
end
end
function addEnemy(e)
enemy = display.newImage('drone.png')
enemy.x = 500
enemy.y = math.floor(math.random()*300)
enemy:scale(-0.1,0.1)
enemy.speed = math.random(2,6)
enemies.insert(enemies,enemy)
enemy.enterFrame = moveEnemy
Runtime:addEventListener('enterFrame',enemy)
end
function moveEnemy(self,event)
--if self.x < 100 then
---self:removeSelf()
--self = nil
--removeSelf()
--else
self.x = self.x-self.speed
--end
end
function update(e)
if(enemies.numChildren ~= 0)then
for i = 1,enemies.numChildren do
if(enemies[i] ~= nil)then
--enemies[i].x = enemies[i].x-3
if(enemies[i].x<100)then
--enemies:remove(enemies[i])
--display.remove(enemies[i])
--enemies[i] = nil
end
end
end
end
end
I have commented out the parts that give me errors.
Any help would be appreciated,
thanks
You might want to try and put the delete code in it's own function and then use a timer to remove it so that the function where you are currently deleting the object can return and you're not deleting yourself.
Another option would be to make it temporarily invisible and then loop through the table periodically and remove anything outside of the move handler.
You have a problem with the following code:
function listeners(action)
if(action == 'add') then
Runtime:addEventListener('enterFrame',update)
enemyTimer = timer.performWithDelay(800,addEnemy,0)
else
Runtime:removeEventListener('enterFrame',update)
timer.cancel(enemyTimer)
end
end
You only want to Runtime:addEventListener('enterFrame', update) once. If you do it everytime you add an enemy things will go wrong. The problem is your code runs again and again after the objects have been removed already.
hmmm. did you use the scene template? if so, you should only put the :removeself () and = nil values in the scene destroy section at the bottom. you also dont need the second removeself that is not attached to an object as that is probably the nil issue.
to summarize.
1-put all remove self () and = nil at the destroy section of scene template and use object.isVisible = false instead.
2-the nil error is most likely coming from the removeself() statement with no object. so change it to moveenemy:removeself ()
if my understanding is right this is how i would do it without using runtime
and using timer instead. ask away if you have question
local scrWidth = display.actualContentWidth
local scrHeight = display.actualContentHeight
local enemy = {} --this will hold your aliens
function addEnemy()
enemy[#enemy + 1] = display.newImage("drone.png")
enemy[#enemy].x = 500
enemy[#enemy].y = math.floor(math.random()*300)
enemy[#enemy]:scale(-0.1,0.1)
enemy[#enemy].speed = math.random(2,6)
end
local function update()
addEnemy()
--this will move the enemy to the left from right
for i=1,#enemy,1 do
enemy[i].x = enemy[i].x - enemy[i].speed
end
--the below codes will destroy each enemy that is out side the screen on the left
local function destroyWhenOutside()
for i=1,#enemy,1 do
if enemy[i].x < 0 - enemy[i].width then
enemy[i]:removeSelf()
enemy[i] = nil
elseif enemy[i].y < 0 - enemy[i].height then
enemy[i]:removeSelf()
enemy[i] = nil
end
end
end
destroyWhenOutside()
end
--this will loop the update every 1/1000 seconds
local timerUpdate = timer.performWithDelay(1,update,-1)
.

Corona, transition.to upon removal from table

Using Corona, i would like to move object when i delete it from a table. The problem is that i iterate the table on every frame. When x > WIDTH - 50 i would like for the monkey to stop moving in a sinuswave form and jump into the removeMonkeys function.
My Code:
local function removeMonkeys(obj)
transition.to(obj, {time = 1500, y = 2*HEIGHT/3, onComplete = obj:removeSelf()})
numMonkeys = numMonkeys - 1;
end
function startGame()
timer.performWithDelay(500, spawn, maxNumMonkeys)
local function onEveryFrame( event )
for i = 1, #monkeySet do
if(monkeySet[i] ~= nil) then
monkeySet[i].x = monkeySet[i].x + 2
monkeySet[i].y = monkeySet[i].y + math.sin(monkeySet[i].x/monkeySpeed)*Amplitude/5
if(monkeySet[i].x > WIDTH -50) then
removeMonkeys(monkeySet[i])
table.remove(monkeySet, i)
print(#monkeySet)
end
end
end
end
Runtime:addEventListener( "enterFrame", onEveryFrame )
Is there anything I'm missing here?
Not sure what exactly you're asking here, but if your transition.to doesn't work check that you aren't you killing the only reference to monkeySet[i] when you do
table.remove(monkeySet, i)

Resources