Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Spawning Object Using Function (Help Me)
Started by sean.sath Jul 27 2018 09:39 PM

2 replies to this topic

Best Answer ldurniat , 28 July 2018 - 08:50 AM

Hi sean.sath,

 

You need (in most cases) add ALL display objects to scene group or one of it child group. There two way you can do this :

 

1. Provide group parameter when you create object e.g.

local obj1 = display.newImageRect(group, "sprites/obj.png", 10, 45)

2. Insert object to group e.g.

group:insert(obj1)

Use scene.view to add objects to scene group e.g. 

local obj1 = display.newImageRect(scene.view, "sprites/obj.png", 10, 45)

This should solve your problem.

 

From Corona documentation :

 

Remember that you must insert scene display objects into the scene's self.view group. If you create display objects but do not insert them into this group, they will reside in front of the Composer stage and they will not be regarded as part of the scene. For display objects which should be part of the scene and "managed" by Composer — for example, cleaned up when the scene is removed — you must insert them into the scene's self.view

 

Read more :

Have a ncie day:)

 

 

ldurniat

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

sean.sath

[GLOBAL: userInfoPane.html]
sean.sath
  • Observer

  • 15 posts
  • Corona SDK

---------------------------------------------------------------------------
local obj1 = display.newImageRect("sprites/obj.png", 10, 45);
	obj1.x = 10 --math.random(0, 300);
	obj1.y = -100  --40;
	physics.addBody( obj1, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } );

local function spawnObj1()
	for i=1,numberObj1 do
	transition.to( obj1, { time=math.random(4000,6000), x=obj1.x , y=500 } ); --, onComplete=clearObj1 } );
   end
end

spawnTimer1 = timer.performWithDelay( spawnTime, spawnObj1, 0 )
---------------------------------------------------------------------------
local function spawnObj2()
	for i=1,numberObj1 do
	local obj1 = display.newImageRect("sprites/obj.png", 10, 45);
	obj1.x = 75 --math.random(0, 300);
	obj1.y = -100  --40;
	transition.to( obj1, { time=math.random(4000,6000), x=obj1.x , y=500, onComplete=clearObj1 } );
	physics.addBody( obj1, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } );
   end
end

spawnTimer2 = timer.performWithDelay( spawnTime, spawnObj2, 0 )

spawnObj2 works the way I want it to but I can't get the objects to remove when I switch scenes. What I tried doing to spawnObj1 was to make the code so that I would be able to add the code group:insert(obj1) I am a noob someone please help me. Here is my menu button (scene switcher) 

function pause(event)
    if(event.phase == "ended") then
	composer.removeScene( "scenes.game", true ) 
	composer.gotoScene( "scenes.menu", "fade", 150)
	timer.cancel (spawnTimer1)
	timer.cancel (spawnTimer2)
    end
end

Here is the whole code. Works fine but objects that are left falling are still there when I switch scenes.

local composer = require('composer')
local scene = composer.newScene()
local physics = require("physics")
physics.start()
--physics.setGravity(0, 10)

--Variables
local numberObj1 = 1 
local spawnTime = 3000

-- Temp Menu Button
local widget =  require "widget"

-- create
function scene:create(event)
    local group = self.view

local background = display.newImageRect("sprites/background.png", display.actualContentWidth, display.actualContentHeight)
	background.x = display.contentCenterX
	background.y = display.contentCenterY

	_W = display.contentWidth
	_H = display.contentHeight
	
local pause = widget.newButton({
    width = 40,
    height = 40,
    defaultFile = "sprites/pause_normal.png",
    overFile = "sprites/pause_hover.png",
    onRelease = pause
		})
    pause.x = display.contentCenterX - 110
    pause.y = display.contentCenterY - 200
--[[
local obj1 = display.newImageRect("sprites/obj.png", 10, 45);
	obj1.x = 10 --math.random(0, 300);
	obj1.y = 0  --40;
	physics.addBody( obj1, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } );

local function spawnObj1()
	for i=1,numberObj1 do
	transition.to( obj1, { time=math.random(4000,6000), x=obj1.x , y=500 } ); --, onComplete=clearObj1 } );
   end
end
--]]
local function spawnObj2()
	for i=1,numberObj1 do
	local obj1 = display.newImageRect("sprites/obj.png", 10, 45);
	obj1.x = 75 --math.random(0, 300);
	obj1.y = 0  --40;
	transition.to( obj1, { time=math.random(4000,6000), x=obj1.x , y=500, onComplete=clearObj1 } );
	physics.addBody( obj1, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } );
   end
end


--Spawner
--spawnTimer1 = timer.performWithDelay( spawnTime, spawnObj1, 0 )
spawnTimer2 = timer.performWithDelay( spawnTime, spawnObj2, 0 )

local function clearObj1( thisObj1 )
   display.remove( thisObj1 ) ; thisObj1 = nil
end
	
	--group:insert(obj1)
	group:insert(background)
	group:insert(pause)
	
end
--Pause
function pause(event)
    if(event.phase == "ended") then
	composer.removeScene( "scenes.game", true )
	composer.gotoScene( "scenes.menu", "fade", 150)
	timer.cancel (spawnTimer1)
	timer.cancel (spawnTimer2)
    end
end

-- Touch listener
local function onTouchEvent(event)
    if event.phase == "began" then
        pause()
    end
end
 
-- show
function scene:show(event)
    local group = self.view
    local phase = event.phase
 
    if (phase == 'will') then
    elseif (phase == 'did') then
    end
end
 
 
-- hide
function scene:hide(event)
    local group = self.view
	
	if pause then
	composer.removeScene("current")

     end
end
 
 
-- destroy
function scene:destroy( event )
    local group = self.view
	local phase = event.phase
	
	if phase == "will" then
	elseif phase == "did" then	
	end
end

scene:addEventListener('create', scene)
scene:addEventListener('show', scene)
scene:addEventListener('hide', scene)
scene:addEventListener('destroy', scene)
 
return scene


[TOPIC: post.html]
#2

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 361 posts
  • Corona SDK

  Best Answer

Hi sean.sath,

 

You need (in most cases) add ALL display objects to scene group or one of it child group. There two way you can do this :

 

1. Provide group parameter when you create object e.g.

local obj1 = display.newImageRect(group, "sprites/obj.png", 10, 45)

2. Insert object to group e.g.

group:insert(obj1)

Use scene.view to add objects to scene group e.g. 

local obj1 = display.newImageRect(scene.view, "sprites/obj.png", 10, 45)

This should solve your problem.

 

From Corona documentation :

 

Remember that you must insert scene display objects into the scene's self.view group. If you create display objects but do not insert them into this group, they will reside in front of the Composer stage and they will not be regarded as part of the scene. For display objects which should be part of the scene and "managed" by Composer — for example, cleaned up when the scene is removed — you must insert them into the scene's self.view

 

Read more :

Have a ncie day:)

 

 

ldurniat


Edited by ldurniat, 28 July 2018 - 08:53 AM.


[TOPIC: post.html]
#3

sean.sath

[GLOBAL: userInfoPane.html]
sean.sath
  • Observer

  • 15 posts
  • Corona SDK

Hi sean.sath,

 

You need (in most cases) add ALL display objects to scene group or one of it child group. There two way you can do this :

 

1. Provide group parameter when you create object e.g.

local obj1 = display.newImageRect(group, "sprites/obj.png", 10, 45)

2. Insert object to group e.g.

group:insert(obj1)

Use scene.view to add objects to scene group e.g. 

local obj1 = display.newImageRect(scene.view, "sprites/obj.png", 10, 45)

This should solve your problem.

 

From Corona documentation :

 

 

Read more :

Have a ncie day:)

 

 

ldurniat

Thank you 




[topic_controls]
[/topic_controls]