Thank you this is very helpful! [import]uid: 225288 topic_id: 36050 reply_id: 143618[/import]
It depends if the cloud is a physics object or not. Let’s assume it’s just a display object, not physics body.
Use a transition over a long period to move the cloud and make it move slower over time:
http://docs.coronalabs.com/api/library/easing/index.html
Examples: http://developer.coronalabs.com/content/easing-examples
Functions: http://developer.coronalabs.com/reference/animation/easing
More: http://developer.coronalabs.com/code/more-easing [import]uid: 8271 topic_id: 36050 reply_id: 143228[/import]
What about using a timer that executes every 60 secs? [import]uid: 50459 topic_id: 36050 reply_id: 143276[/import]
the full code is
[code]
local Cloudtable = {} – Set up Cloud table
function initCloud()
local Cloud1 = {}
Cloud1.imgpath = “Cloud1.png”; --Set Image Path for Cloud
Cloud1.movementSpeed = 10000; --Determines the movement speed of Cloud
table.insert(Cloudtable, Cloud1); --Insert Cloud into Cloudtable
local Cloud2 = {}
Cloud2.imgpath = “Cloud2.png”;
Cloud2.movementSpeed = 12000;
table.insert(Cloudtable, Cloud2);
local Cloud3 = {}
Cloud3.imgpath = “Cloud3.png”;
Cloud3.movementSpeed = 14000;
table.insert(Cloudtable, Cloud3);
local Cloud4 = {}
Cloud4.imgpath = “Cloud4.png”;
Cloud4.movementSpeed = 16000;
table.insert(Cloudtable, Cloud4);
local Cloud5 = {}
Cloud5.imgpath = “Cloud5.png”;
Cloud5.movementSpeed = 18000;
table.insert(Cloudtable, Cloud5);
end --END initCloud()
function getRandomCloud()
local temp = Cloudtable[math.random(1, #Cloudtable)] – Get a random Cloud from Cloudtable
local randomCloud = display.newImage(temp.imgpath) – Set image path for object
if ( temp.imgpath == “Cloud4.png” ) then
physics.addBody( randomCloud, “static”, { density=.1, bounce=.1, friction=.2, radius=45 } )
end
randomCloud.myName = “Cloud” – Set the name of the object to Cloud
randomCloud.movementSpeed = temp.movementSpeed; – Set how fast the object will move
randomCloud.x = math.random(10, _W);
randomCloud.y = -35;
randomCloud.rotation = math.random(0,20) – Rotate the object
CloudMove = transition.to(randomCloud, {
time=randomCloud.movementSpeed,
y=500,
onComplete = function(self) self.parent:remove(self); self = nil; end
}) – Move the Cloud
end–END getRandomCloud()
function CloudtGame()
Cloudtimer1 = timer.performWithDelay(1000,getRandomCloud, 7)
Cloudtimer2 = timer.performWithDelay(2000,getRandomCloud, 0)
Cloudtimer3 = timer.performWithDelay(3000,getRandomCloud, 0)
Cloudtimer4 = timer.performWithDelay(4000,getRandomCloud, 0)
Cloudtimer5 = timer.performWithDelay(5000,getRandomCloud, 0)
end–END CloudtGame()
initCloud()
startGame()
[/code] [import]uid: 225288 topic_id: 36050 reply_id: 143316[/import]
Ok, a number of things:
I don’t think you should be creating multiple cloud objects in the initialiser. Instead, have a function which creates one cloud object that you can call multiple times. Each cloud object can be added to a clouds display group and (if you really want to) a clouds table, for management. Remember that display groups can be iterated just like tables, you just need to loop from 1 to .numChildren instead of 1 to #table.
Unless the clouds need to interact with other physics objects, they should not be static physics bodies. This lightens the load on the CPU and your management of the code. If they do need to be physics bodies, you should really ask yourself if they need to be just sensors or not. I think you’ll find they are ok to be just display objects without physics. This also makes it easier to manage their movement.
The timers for each cloud should be created when the clouds are created, not separately. If you follow my first point you’ll be able to reduce this code complexity as well. You can move the handle to the timer into the cloud object and reduce the management of cloud timers there.
It’s good that you’re using transition.to() but I think that this should be setup in the init function, as well.
There is a lot of reduction to be had in your code. It’s not that it’s bad, per se, but it can do with some tidying up. Think about how you would use a single function to create a cloud and how it would keep references to all cloud data within the object created in that function. Something like:
[lua]
– contains the clouds
local clouds = display.newGroup()
– creates a cloud
local function newCloud(name)
– load cloud image
local cloud = display.newImage( clouds, name )
– start cloud moving and restart cloud moving
local function resetCloud()
– position cloud at random height off the left of the screen
cloud.x, cloud.y = -100, math.random(0, 200)
– random speed for the cloud to move
cloud.speed = math.random(3,10) * 1000
– create cloud timer (use this to kill the timer when the clouds are destroyed)
cloud.timer = transition.to(cloud,{
time=cloud.speed,x=display.contentWidth+100,onComplete=resetCloud
})
end
– start the cloud moving
resetCloud()
– if necessary, the cloud can be referenced later on
return cloud
end
– create 5 clouds at random positions
for i=1, 5 do
newCloud(“cloud”…i…".png")
end
– when the scene ends you can kill all the clouds (loop backwards!)
local function killClouds()
for i=clouds.numChildren, 1, -1 do
timer.cancel( clouds[i].timer )
clouds[i]:removeSelf()
end
end
[/lua]
[import]uid: 8271 topic_id: 36050 reply_id: 143319[/import]
yes but i need a body on the clouds. have tried this however
-- requires
display.setStatusBar( display.HiddenStatusBar )
\_W = display.contentWidth; --Returns Screen Width
\_H = display.contentHeight; --Returns Screen Height
local starTable = {} -- Set up star table
local physics = require "physics"
physics.start()
function initStar()
local star1 = {}
star1.imgpath = "Star1.png"; --Set Image Path for Star
star1.movementSpeed = 10000; --Determines the movement speed of star
table.insert(starTable, star1); --Insert Star into starTable
end --END initStar()
local function star1incr()
star1.movementSpeed = star1.movementSpeed - 1
print( "- 1" )
end
timer.performWithDelay(10000, star1incr, 0)
function getRandomStar()
local temp = starTable[math.random(1, #starTable)] -- Get a random star from starTable
local randomStar = display.newImage(temp.imgpath) -- Set image path for object
if ( temp.imgpath == "Cloud4.png" ) then
physics.addBody( randomStar, "static", { density=.1, bounce=.1, friction=.2, radius=45 } )
end
randomStar.myName = "star" -- Set the name of the object to star
randomStar.movementSpeed = temp.movementSpeed; -- Set how fast the object will move
randomStar.x = math.random(10, \_W);
randomStar.y = -35;
randomStar.rotation = math.random(0,20) -- Rotate the object
starMove = transition.to(randomStar, {
time=randomStar.movementSpeed,
y=500,
onComplete = function(self) self.parent:remove(self); self = nil; end
}) -- Move the star
end--END getRandomStar()
function startGame()
starTimer1 = timer.performWithDelay(1000,getRandomStar, 0)
end--END startGame()
initStar()
startGame()
I get this error: \main.lua:18: attempt to index global ‘star1’ (a nil value) [import]uid: 225288 topic_id: 36050 reply_id: 143567[/import]
You get that error because you have not declared or defined a variable called start1 by the time line 18 executes.
Why do you need a body on the cloud? You’ve stated that, but not explained it. What is the body for? [import]uid: 8271 topic_id: 36050 reply_id: 143569[/import]
One of the clouds needs to interact with the player, to call a collision function.
if ( temp.imgpath == "Cloud4.png" ) then
physics.addBody( randomStar, "static", { density=.1, bounce=.1, friction=.2, radius=45 } )
end
[import]uid: 225288 topic_id: 36050 reply_id: 143572[/import]
fixed it by
local function star1incr()
starTable[1].movementSpeed = starTable[1].movementSpeed - 1
--print( "- 1" )
end
[import]uid: 225288 topic_id: 36050 reply_id: 143579[/import]
@mmk.verheijden
You don’t always need a physics object to be able to use collision detection. Here’s a small routine that checks if 2 images collide/touch each other:
function hitTestObjects(obj1, obj2)
return obj1.contentBounds.xMin \< obj2.contentBounds.xMax
and obj1.contentBounds.xMax \> obj2.contentBounds.xMin
and obj1.contentBounds.yMin \< obj2.contentBounds.yMax
and obj1.contentBounds.yMax \> obj2.contentBounds.yMin
end
Use it like this:
if hitTestObjects(Cloud,Player) == true then
--- Cloud hits Player, do something
end
BTW Found this code in the Code Exchange somewhere.
[import]uid: 50459 topic_id: 36050 reply_id: 143600[/import]
Thank you this is very helpful! [import]uid: 225288 topic_id: 36050 reply_id: 143618[/import]
It depends if the cloud is a physics object or not. Let’s assume it’s just a display object, not physics body.
Use a transition over a long period to move the cloud and make it move slower over time:
http://docs.coronalabs.com/api/library/easing/index.html
Examples: http://developer.coronalabs.com/content/easing-examples
Functions: http://developer.coronalabs.com/reference/animation/easing
More: http://developer.coronalabs.com/code/more-easing [import]uid: 8271 topic_id: 36050 reply_id: 143228[/import]
What about using a timer that executes every 60 secs? [import]uid: 50459 topic_id: 36050 reply_id: 143276[/import]
the full code is
[code]
local Cloudtable = {} – Set up Cloud table
function initCloud()
local Cloud1 = {}
Cloud1.imgpath = “Cloud1.png”; --Set Image Path for Cloud
Cloud1.movementSpeed = 10000; --Determines the movement speed of Cloud
table.insert(Cloudtable, Cloud1); --Insert Cloud into Cloudtable
local Cloud2 = {}
Cloud2.imgpath = “Cloud2.png”;
Cloud2.movementSpeed = 12000;
table.insert(Cloudtable, Cloud2);
local Cloud3 = {}
Cloud3.imgpath = “Cloud3.png”;
Cloud3.movementSpeed = 14000;
table.insert(Cloudtable, Cloud3);
local Cloud4 = {}
Cloud4.imgpath = “Cloud4.png”;
Cloud4.movementSpeed = 16000;
table.insert(Cloudtable, Cloud4);
local Cloud5 = {}
Cloud5.imgpath = “Cloud5.png”;
Cloud5.movementSpeed = 18000;
table.insert(Cloudtable, Cloud5);
end --END initCloud()
function getRandomCloud()
local temp = Cloudtable[math.random(1, #Cloudtable)] – Get a random Cloud from Cloudtable
local randomCloud = display.newImage(temp.imgpath) – Set image path for object
if ( temp.imgpath == “Cloud4.png” ) then
physics.addBody( randomCloud, “static”, { density=.1, bounce=.1, friction=.2, radius=45 } )
end
randomCloud.myName = “Cloud” – Set the name of the object to Cloud
randomCloud.movementSpeed = temp.movementSpeed; – Set how fast the object will move
randomCloud.x = math.random(10, _W);
randomCloud.y = -35;
randomCloud.rotation = math.random(0,20) – Rotate the object
CloudMove = transition.to(randomCloud, {
time=randomCloud.movementSpeed,
y=500,
onComplete = function(self) self.parent:remove(self); self = nil; end
}) – Move the Cloud
end–END getRandomCloud()
function CloudtGame()
Cloudtimer1 = timer.performWithDelay(1000,getRandomCloud, 7)
Cloudtimer2 = timer.performWithDelay(2000,getRandomCloud, 0)
Cloudtimer3 = timer.performWithDelay(3000,getRandomCloud, 0)
Cloudtimer4 = timer.performWithDelay(4000,getRandomCloud, 0)
Cloudtimer5 = timer.performWithDelay(5000,getRandomCloud, 0)
end–END CloudtGame()
initCloud()
startGame()
[/code] [import]uid: 225288 topic_id: 36050 reply_id: 143316[/import]
Ok, a number of things:
I don’t think you should be creating multiple cloud objects in the initialiser. Instead, have a function which creates one cloud object that you can call multiple times. Each cloud object can be added to a clouds display group and (if you really want to) a clouds table, for management. Remember that display groups can be iterated just like tables, you just need to loop from 1 to .numChildren instead of 1 to #table.
Unless the clouds need to interact with other physics objects, they should not be static physics bodies. This lightens the load on the CPU and your management of the code. If they do need to be physics bodies, you should really ask yourself if they need to be just sensors or not. I think you’ll find they are ok to be just display objects without physics. This also makes it easier to manage their movement.
The timers for each cloud should be created when the clouds are created, not separately. If you follow my first point you’ll be able to reduce this code complexity as well. You can move the handle to the timer into the cloud object and reduce the management of cloud timers there.
It’s good that you’re using transition.to() but I think that this should be setup in the init function, as well.
There is a lot of reduction to be had in your code. It’s not that it’s bad, per se, but it can do with some tidying up. Think about how you would use a single function to create a cloud and how it would keep references to all cloud data within the object created in that function. Something like:
[lua]
– contains the clouds
local clouds = display.newGroup()
– creates a cloud
local function newCloud(name)
– load cloud image
local cloud = display.newImage( clouds, name )
– start cloud moving and restart cloud moving
local function resetCloud()
– position cloud at random height off the left of the screen
cloud.x, cloud.y = -100, math.random(0, 200)
– random speed for the cloud to move
cloud.speed = math.random(3,10) * 1000
– create cloud timer (use this to kill the timer when the clouds are destroyed)
cloud.timer = transition.to(cloud,{
time=cloud.speed,x=display.contentWidth+100,onComplete=resetCloud
})
end
– start the cloud moving
resetCloud()
– if necessary, the cloud can be referenced later on
return cloud
end
– create 5 clouds at random positions
for i=1, 5 do
newCloud(“cloud”…i…".png")
end
– when the scene ends you can kill all the clouds (loop backwards!)
local function killClouds()
for i=clouds.numChildren, 1, -1 do
timer.cancel( clouds[i].timer )
clouds[i]:removeSelf()
end
end
[/lua]
[import]uid: 8271 topic_id: 36050 reply_id: 143319[/import]
yes but i need a body on the clouds. have tried this however
-- requires
display.setStatusBar( display.HiddenStatusBar )
\_W = display.contentWidth; --Returns Screen Width
\_H = display.contentHeight; --Returns Screen Height
local starTable = {} -- Set up star table
local physics = require "physics"
physics.start()
function initStar()
local star1 = {}
star1.imgpath = "Star1.png"; --Set Image Path for Star
star1.movementSpeed = 10000; --Determines the movement speed of star
table.insert(starTable, star1); --Insert Star into starTable
end --END initStar()
local function star1incr()
star1.movementSpeed = star1.movementSpeed - 1
print( "- 1" )
end
timer.performWithDelay(10000, star1incr, 0)
function getRandomStar()
local temp = starTable[math.random(1, #starTable)] -- Get a random star from starTable
local randomStar = display.newImage(temp.imgpath) -- Set image path for object
if ( temp.imgpath == "Cloud4.png" ) then
physics.addBody( randomStar, "static", { density=.1, bounce=.1, friction=.2, radius=45 } )
end
randomStar.myName = "star" -- Set the name of the object to star
randomStar.movementSpeed = temp.movementSpeed; -- Set how fast the object will move
randomStar.x = math.random(10, \_W);
randomStar.y = -35;
randomStar.rotation = math.random(0,20) -- Rotate the object
starMove = transition.to(randomStar, {
time=randomStar.movementSpeed,
y=500,
onComplete = function(self) self.parent:remove(self); self = nil; end
}) -- Move the star
end--END getRandomStar()
function startGame()
starTimer1 = timer.performWithDelay(1000,getRandomStar, 0)
end--END startGame()
initStar()
startGame()
I get this error: \main.lua:18: attempt to index global ‘star1’ (a nil value) [import]uid: 225288 topic_id: 36050 reply_id: 143567[/import]
You get that error because you have not declared or defined a variable called start1 by the time line 18 executes.
Why do you need a body on the cloud? You’ve stated that, but not explained it. What is the body for? [import]uid: 8271 topic_id: 36050 reply_id: 143569[/import]
One of the clouds needs to interact with the player, to call a collision function.
if ( temp.imgpath == "Cloud4.png" ) then
physics.addBody( randomStar, "static", { density=.1, bounce=.1, friction=.2, radius=45 } )
end
[import]uid: 225288 topic_id: 36050 reply_id: 143572[/import]
fixed it by
local function star1incr()
starTable[1].movementSpeed = starTable[1].movementSpeed - 1
--print( "- 1" )
end
[import]uid: 225288 topic_id: 36050 reply_id: 143579[/import]
@mmk.verheijden
You don’t always need a physics object to be able to use collision detection. Here’s a small routine that checks if 2 images collide/touch each other:
function hitTestObjects(obj1, obj2)
return obj1.contentBounds.xMin \< obj2.contentBounds.xMax
and obj1.contentBounds.xMax \> obj2.contentBounds.xMin
and obj1.contentBounds.yMin \< obj2.contentBounds.yMax
and obj1.contentBounds.yMax \> obj2.contentBounds.yMin
end
Use it like this:
if hitTestObjects(Cloud,Player) == true then
--- Cloud hits Player, do something
end
BTW Found this code in the Code Exchange somewhere.
[import]uid: 50459 topic_id: 36050 reply_id: 143600[/import]