How to do this?

hi, i have a little problem im working on my first project is a game with a rocket in the space, the rocket has to evade the enemies, the rocket only moves up and down, and i have a code from a user here on this page but im only understand it from parts, is a code of a line following the rocket like a fire leaving behind. my problem is when i reload the scene with my reload button all of my objects works perfectly but the line starts to paint for all the screen.

i think my problem is when i reload all the scene im not reloading the line and its paints again, but i don’t know how to reload the line like my other objects

this is an example of my image group in the scene.

local storyboard = require ("storyboard") local scene = storyboard.newScene() function scene:createScene(event) local screenGroup = self.view object = display.newImage("rocket.png") object:scale(0.023,0.023) object.x = 113 object.y = 160 physics.addBody( object, "dynamic", physicsData:get("rocket") ) screenGroup:insert(object) end

and this is the code of the line following the rocket

xpos = { x = 0 } -- A proxy object for the exhaust local my\_line, prevx, prevy local X = 90 local TimePerScreen = 2000 local NumberOfScreens = 2 local FinalX = -NumberOfScreens \* display.contentWidth transition.to(xpos, { time = TimePerScreen \* NumberOfScreens, x = FinalX, iterations = -1, -- Repeat forever onRepeat = function() transition.to(my\_line, { -- Move the old line the rest of the way off screen... time = TimePerScreen, x = my\_line.x - display.contentWidth, onComplete = my\_line:removeSelf() }) my\_line = nil -- ...and start a new one. prevx = prevx - FinalX -- Correct the previous x to account for the split end }) timer.performWithDelay(5, function() local x, y = xpos.x, object.y if x == FinalX then -- x might still be a big negative value for a few frames x = 0 end if x ~= prevx or y ~= prevy then if my\_line then -- line already exists? my\_line.x = X + x my\_line:append(X, y) elseif prevx then -- previous point exists? my\_line = display.newLine(X + prevx, prevy, X, y) my\_line:setStrokeColor( 0,0.6,0.9 ) my\_line.strokeWidth = 20 my\_line.alpha= 0.3 end prevx, prevy = x, y end end, 0) function scene:enterScene(event) end function scene:exitScene(event) end function scene:destroyScene(event) end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene

My question is how to put the object my_line in the image group to reload it with my other objects together

sorry if i not express my self correctly im learning english.

You should insert all scene objects (including your line) to scene group, like you did here:

[lua]

screenGroup:insert(object)

[/lua]

Daniel

Hello Daniel, thank you for your answer but if i do that screenGroup:insert(my_line) i have this error

attempt to call method ‘append’ (a nil value) stack traceback

pdd. the code of my_line only throw me the error when i reload the scene.

Hi!

Sorry, I gave you something for demonstration purposes, so it does need a little work.  :slight_smile:

In addition to what Daniel says, you should get the handles to those control objects. So these lines change:

local xpos\_transition = transition.to(xpos, { -- ... local exhaust\_timer = timer.performWithDelay(5, function()

You don’t want some of that stuff sticking around between scenes. These objects go on forever, if not stopped, so they’re a problem. Also, some of that “prev” stuff assumes the line is still intact. So in your exitScene (or hide , in Composer), you then want to do:

timer.cancel(exhaust\_timer) transition.cancel(xpos\_transition) display.remove(my\_line) -- probably exists, but account for first two frames or so xpos.x = 0 exhaust\_timer, xpos\_transition, my\_line, prevx, prevy = nil

Also, then you would make the transition and timer each time, in enterScene (or show , in Composer), instead of once when you require the file. In that case, it might be easier to stuff these values into your scene (say, scene.my_line instead of my_line ).

Hi starCrunch thank you for helping me again :slight_smile: i follow your instructions and this is my code now

local storyboard = require ("storyboard") local scene = storyboard.newScene() function scene:createScene(event) local screenGroup = self.view object = display.newImage("rocket.png") object:scale(0.023,0.023) object.x = 113 object.y = 160 physics.addBody( object, "dynamic", physicsData:get("rocket") ) screenGroup:insert(object) xpos = { x = 0 } -- A proxy object for the exhaust local my\_line, prevx, prevy local X = 90 local TimePerScreen = 2000 local NumberOfScreens = 2 local FinalX = -NumberOfScreens \* display.contentWidth local xpos\_transition = transition.to(xpos, { time = TimePerScreen \* NumberOfScreens, x = FinalX, iterations = -1, -- Repeat forever onRepeat = function() transition.to(my\_line, { -- Move the old line the rest of the way off screen... time = TimePerScreen, x = my\_line.x - display.contentWidth, onComplete = my\_line:removeSelf() }) my\_line = nil -- ...and start a new one. prevx = prevx - FinalX -- Correct the previous x to account for the split end }) local exhaust\_timer = timer.performWithDelay(5, function() local x, y = xpos.x, object.y if x == FinalX then -- x might still be a big negative value for a few frames x = 0 end if x ~= prevx or y ~= prevy then if my\_line then -- line already exists? my\_line.x = X + x my\_line:append(X, y) elseif prevx then -- previous point exists? my\_line = display.newLine(X + prevx, prevy, X, y) my\_line:setStrokeColor( 0,0.6,0.9 ) my\_line.strokeWidth = 20 my\_line.alpha= 0.3 screenGroup:insert(my\_line) end prevx, prevy = x, y end end, 0) end function scene:enterScene(event) end function scene:exitScene(event) timer.cancel(exhaust\_timer)transition.cancel(xpos\_transition) display.remove(my\_line) -- probably exists, but account for first two frames or so xpos.x = 0 exhaust\_timer, xpos\_transition, my\_line, prevx, prevy = nil endend function scene:destroyScene(event) end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene

when the games retry i got this error

attempt to index a nil value

stack traceback:

in function ‘cancel’

what im doing wrong?

thank u again

Hi.

I’m about to go to sleep, but I’ll try to quickly give you some hints.

First, you probably want that stuff in enterScene , not createScene.

As for the error, that’s why I mentioned that you might want to put those in the scene instead (the note about scene.my_line , for example). In your code right now, the local variables are stuck inside the scope of createScene , and exitScene can’t see them.

sorry for the time, and thank you again for your help im now understand  what are you saying, and sorry for my ignorance im learning and i want to learn :slight_smile: have a great night!

You should insert all scene objects (including your line) to scene group, like you did here:

[lua]

screenGroup:insert(object)

[/lua]

Daniel

Hello Daniel, thank you for your answer but if i do that screenGroup:insert(my_line) i have this error

attempt to call method ‘append’ (a nil value) stack traceback

pdd. the code of my_line only throw me the error when i reload the scene.

Hi!

Sorry, I gave you something for demonstration purposes, so it does need a little work.  :slight_smile:

In addition to what Daniel says, you should get the handles to those control objects. So these lines change:

local xpos\_transition = transition.to(xpos, { -- ... local exhaust\_timer = timer.performWithDelay(5, function()

You don’t want some of that stuff sticking around between scenes. These objects go on forever, if not stopped, so they’re a problem. Also, some of that “prev” stuff assumes the line is still intact. So in your exitScene (or hide , in Composer), you then want to do:

timer.cancel(exhaust\_timer) transition.cancel(xpos\_transition) display.remove(my\_line) -- probably exists, but account for first two frames or so xpos.x = 0 exhaust\_timer, xpos\_transition, my\_line, prevx, prevy = nil

Also, then you would make the transition and timer each time, in enterScene (or show , in Composer), instead of once when you require the file. In that case, it might be easier to stuff these values into your scene (say, scene.my_line instead of my_line ).

Hi starCrunch thank you for helping me again :slight_smile: i follow your instructions and this is my code now

local storyboard = require ("storyboard") local scene = storyboard.newScene() function scene:createScene(event) local screenGroup = self.view object = display.newImage("rocket.png") object:scale(0.023,0.023) object.x = 113 object.y = 160 physics.addBody( object, "dynamic", physicsData:get("rocket") ) screenGroup:insert(object) xpos = { x = 0 } -- A proxy object for the exhaust local my\_line, prevx, prevy local X = 90 local TimePerScreen = 2000 local NumberOfScreens = 2 local FinalX = -NumberOfScreens \* display.contentWidth local xpos\_transition = transition.to(xpos, { time = TimePerScreen \* NumberOfScreens, x = FinalX, iterations = -1, -- Repeat forever onRepeat = function() transition.to(my\_line, { -- Move the old line the rest of the way off screen... time = TimePerScreen, x = my\_line.x - display.contentWidth, onComplete = my\_line:removeSelf() }) my\_line = nil -- ...and start a new one. prevx = prevx - FinalX -- Correct the previous x to account for the split end }) local exhaust\_timer = timer.performWithDelay(5, function() local x, y = xpos.x, object.y if x == FinalX then -- x might still be a big negative value for a few frames x = 0 end if x ~= prevx or y ~= prevy then if my\_line then -- line already exists? my\_line.x = X + x my\_line:append(X, y) elseif prevx then -- previous point exists? my\_line = display.newLine(X + prevx, prevy, X, y) my\_line:setStrokeColor( 0,0.6,0.9 ) my\_line.strokeWidth = 20 my\_line.alpha= 0.3 screenGroup:insert(my\_line) end prevx, prevy = x, y end end, 0) end function scene:enterScene(event) end function scene:exitScene(event) timer.cancel(exhaust\_timer)transition.cancel(xpos\_transition) display.remove(my\_line) -- probably exists, but account for first two frames or so xpos.x = 0 exhaust\_timer, xpos\_transition, my\_line, prevx, prevy = nil endend function scene:destroyScene(event) end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene

when the games retry i got this error

attempt to index a nil value

stack traceback:

in function ‘cancel’

what im doing wrong?

thank u again

Hi.

I’m about to go to sleep, but I’ll try to quickly give you some hints.

First, you probably want that stuff in enterScene , not createScene.

As for the error, that’s why I mentioned that you might want to put those in the scene instead (the note about scene.my_line , for example). In your code right now, the local variables are stuck inside the scope of createScene , and exitScene can’t see them.

sorry for the time, and thank you again for your help im now understand  what are you saying, and sorry for my ignorance im learning and i want to learn :slight_smile: have a great night!