Need help with logic, please.

Hello. I have a brain freeze and am not sure how to proceed with what I am doing. Im confused as to the logic.

I want this to happen -

Object A is in the center of the screen as Object B, C and D “fly” into view randomly targeting Object A.

Im having trouble thinking what to write to make the Objects B, C, and D “fly” into view randomly. The Objects in this case are the enemies, which are all sprite sheets. 3 sprite sheets… I am encapsulating everything within the default storyboard template. :wink:

I guess I am asking for some help on how to start this out. Any suggestions are very much appreciated. My confidence needs a boost…Im feeling a bit stuck and I dont drink coffee, so that’s out of the question, lol… :stuck_out_tongue:

Thanks in advance for any help.

Create a method to spawn the objects. (of course you can spawn as many objects as you want)

[

local _W = display.contentWidth

local _H = display.contentHeight

local mRand = math.random

– SPRITE SHEETS (assumes all the sprite sheets are same dimensions)

local optionsObject = { width = 64, height = 64, numFrames = 4, sheetContentWidth = 256, sheetContentHeight = 256  }

local object1_Sheet = graphics.newImageSheet(“Textures/obj1.png”, optionsObject)

local object2_Sheet = graphics.newImageSheet(“Textures/obj2.png”, optionsObject)

local object3_Sheet = graphics.newImageSheet(“Textures/obj3.png”, optionsObject)

sequenceData = { name = “moving”, start = 1, count = 4, time = 100 } 

– CREATE A TABLE OF ENEMIES (3 or 100 or as many as you want)

local enemyID = 1

local enemies = {}

for i = 1, 100 do

    local index = mRand(1,3)

    if index == 1 then

        enemies[i] = display.newSprite(object1_Sheet,sequenceData)

    elseif index == 2 then

        enemies[i] = display.newSprite(object2_Sheet,sequenceData)

    elseif index == 3 then

        enemies[i] = display.newSprite(object3_Sheet,sequenceData)

    end

end

local function spawn(  )

   

   local screenEdge = mRand( 1, 4)

   if screenEdge == 1 then  – top of screen

        enemies[enemyID].x = mRand(50, _W-50)

        enemies[enemyID].y = -50

   elseif screeEdge == 2 then – right side of screen

         enemies[enemyID].x = _W+50

        enemies[enemyID].y = mRand(50, _H-50)

  elseif screeEdge == 3 then – bottom side of screen

       enemies[enemyID].x = mRand(50, _W-50)

       enemies[enemyID].y = _H+50

  elseif screeEdge == 4 then – left side of screen

       enemies[enemyID].x = _W-50

       enemies[enemyID].y = mRand(50, _H-50)

  end

  

  enemyID = enemyID + 1

  

end

– SPAWN 100 ENEMIES

timer.performWithDelay(1000, spawn, 100)

]

Of course you can change this to create the enemies in the spawn method instead of creating the 100 enemies ahead of time.

In either case, you should have a method call that will handle cleaning up the enemy objects once they are destroyed, no longer needed, and if you want to destroy/save/pause the enemies when you leave the present storyboard scene.  But I only had time to slap together this example, just to directly answer your immediate question.  

As far as using within storyboard, it makes sense you would do all this in the ‘gameScene’ or ‘levelScene’ whatever you titled the main scene of your app where all this action is to occur.

Good luck!

Hope this helps you with you question.

Create a method to spawn the objects. (of course you can spawn as many objects as you want)

 

local \_W = display.contentWidth local \_H = display.contentHeight local mRand = math.random   -- SPRITE SHEETS (assumes all the sprite sheets are same dimensions) local optionsObject = { width = 64, height = 64, numFrames = 4, sheetContentWidth = 256, sheetContentHeight = 256  } local object1\_Sheet = graphics.newImageSheet("Textures/obj1.png", optionsObject) local object2\_Sheet = graphics.newImageSheet("Textures/obj2.png", optionsObject) local object3\_Sheet = graphics.newImageSheet("Textures/obj3.png", optionsObject) sequenceData = { name = "moving", start = 1, count = 4, time = 100 }    -- CREATE A TABLE OF ENEMIES (3 or 100 or as many as you want) local enemyID = 1 local enemies = {} for i = 1, 100 do     local index = mRand(1,3)     if index == 1 then         enemies[i] = display.newSprite(object1\_Sheet,sequenceData)     elseif index == 2 then         enemies[i] = display.newSprite(object2\_Sheet,sequenceData)     elseif index == 3 then         enemies[i] = display.newSprite(object3\_Sheet,sequenceData)     end end     local function spawn(  )        local screenEdge = mRand( 1, 4)    if screenEdge == 1 then  -- top of screen         enemies[enemyID].x = mRand(50, \_W-50)         enemies[enemyID].y = -50    elseif screeEdge == 2 then -- right side of screen          enemies[enemyID].x = \_W+50         enemies[enemyID].y = mRand(50, \_H-50)   elseif screeEdge == 3 then -- bottom side of screen        enemies[enemyID].x = mRand(50, \_W-50)        enemies[enemyID].y = \_H+50   elseif screeEdge == 4 then -- left side of screen        enemies[enemyID].x = \_W-50        enemies[enemyID].y = mRand(50, \_H-50)   end      enemyID = enemyID + 1    end   -- SPAWN 100 ENEMIES timer.performWithDelay(1000, spawn, 100)  

 

Of course you can change this to create the enemies in the spawn method instead of creating the 100 enemies ahead of time.

 

In either case, you should have a method call that will handle cleaning up the enemy objects once they are destroyed, no longer needed, and if you want to destroy/save/pause the enemies when you leave the present storyboard scene.  But I only had time to slap together this example, just to directly answer your immediate question.  

 

As far as using within storyboard, it makes sense you would do all this in the ‘gameScene’ or ‘levelScene’ whatever you titled the main scene of your app where all this action is to occur.

 

Good luck!

 

Hope this helps you with you question.

Thanks for that post. I think I was having the biggest problem with the sprite sheets. I know how to spawn still images, but sprites don’t work the same way (at least not in my case). So looks like study time…

Thanks again for that sample. :slight_smile:

I do think they work the same as still images; but that is if you are using the newImageSheet api and not the older sprite stuff from way back when.

If you are not using the  ‘newImageSheet’ and the ‘newSprite’, you should be.  There are a few good tutorials on corona site on these.

Once you are using these, spawning and moving the sprite object is the same as a still image!!!

just do this :

– start the animation loop

enemies[1]:play()  

– move the enemy

transition.to( enemies[1], {time = 2000, x = _W/2, y = _H/2 })

This will move the enemy object from it’s random spawn location to the center of the screen

of course, it sounds like you have enough corona knowledge to figure how to transition it to whatever location 

you need to, but it really is the same as a standard display object(still image).

Of course, you can also incrementally move the enemy unit in an ‘enterFrame’ event, instead of the transition call;

but you are most likely already aware of that 

Study time is always a good thing… just don’t get stuck studying the older sprite api, it is outdated.

Good luck!

Create a method to spawn the objects. (of course you can spawn as many objects as you want)

[

local _W = display.contentWidth

local _H = display.contentHeight

local mRand = math.random

– SPRITE SHEETS (assumes all the sprite sheets are same dimensions)

local optionsObject = { width = 64, height = 64, numFrames = 4, sheetContentWidth = 256, sheetContentHeight = 256  }

local object1_Sheet = graphics.newImageSheet(“Textures/obj1.png”, optionsObject)

local object2_Sheet = graphics.newImageSheet(“Textures/obj2.png”, optionsObject)

local object3_Sheet = graphics.newImageSheet(“Textures/obj3.png”, optionsObject)

sequenceData = { name = “moving”, start = 1, count = 4, time = 100 } 

– CREATE A TABLE OF ENEMIES (3 or 100 or as many as you want)

local enemyID = 1

local enemies = {}

for i = 1, 100 do

    local index = mRand(1,3)

    if index == 1 then

        enemies[i] = display.newSprite(object1_Sheet,sequenceData)

    elseif index == 2 then

        enemies[i] = display.newSprite(object2_Sheet,sequenceData)

    elseif index == 3 then

        enemies[i] = display.newSprite(object3_Sheet,sequenceData)

    end

end

local function spawn(  )

   

   local screenEdge = mRand( 1, 4)

   if screenEdge == 1 then  – top of screen

        enemies[enemyID].x = mRand(50, _W-50)

        enemies[enemyID].y = -50

   elseif screeEdge == 2 then – right side of screen

         enemies[enemyID].x = _W+50

        enemies[enemyID].y = mRand(50, _H-50)

  elseif screeEdge == 3 then – bottom side of screen

       enemies[enemyID].x = mRand(50, _W-50)

       enemies[enemyID].y = _H+50

  elseif screeEdge == 4 then – left side of screen

       enemies[enemyID].x = _W-50

       enemies[enemyID].y = mRand(50, _H-50)

  end

  

  enemyID = enemyID + 1

  

end

– SPAWN 100 ENEMIES

timer.performWithDelay(1000, spawn, 100)

]

Of course you can change this to create the enemies in the spawn method instead of creating the 100 enemies ahead of time.

In either case, you should have a method call that will handle cleaning up the enemy objects once they are destroyed, no longer needed, and if you want to destroy/save/pause the enemies when you leave the present storyboard scene.  But I only had time to slap together this example, just to directly answer your immediate question.  

As far as using within storyboard, it makes sense you would do all this in the ‘gameScene’ or ‘levelScene’ whatever you titled the main scene of your app where all this action is to occur.

Good luck!

Hope this helps you with you question.

Create a method to spawn the objects. (of course you can spawn as many objects as you want)

 

local \_W = display.contentWidth local \_H = display.contentHeight local mRand = math.random   -- SPRITE SHEETS (assumes all the sprite sheets are same dimensions) local optionsObject = { width = 64, height = 64, numFrames = 4, sheetContentWidth = 256, sheetContentHeight = 256  } local object1\_Sheet = graphics.newImageSheet("Textures/obj1.png", optionsObject) local object2\_Sheet = graphics.newImageSheet("Textures/obj2.png", optionsObject) local object3\_Sheet = graphics.newImageSheet("Textures/obj3.png", optionsObject) sequenceData = { name = "moving", start = 1, count = 4, time = 100 }    -- CREATE A TABLE OF ENEMIES (3 or 100 or as many as you want) local enemyID = 1 local enemies = {} for i = 1, 100 do     local index = mRand(1,3)     if index == 1 then         enemies[i] = display.newSprite(object1\_Sheet,sequenceData)     elseif index == 2 then         enemies[i] = display.newSprite(object2\_Sheet,sequenceData)     elseif index == 3 then         enemies[i] = display.newSprite(object3\_Sheet,sequenceData)     end end     local function spawn(  )        local screenEdge = mRand( 1, 4)    if screenEdge == 1 then  -- top of screen         enemies[enemyID].x = mRand(50, \_W-50)         enemies[enemyID].y = -50    elseif screeEdge == 2 then -- right side of screen          enemies[enemyID].x = \_W+50         enemies[enemyID].y = mRand(50, \_H-50)   elseif screeEdge == 3 then -- bottom side of screen        enemies[enemyID].x = mRand(50, \_W-50)        enemies[enemyID].y = \_H+50   elseif screeEdge == 4 then -- left side of screen        enemies[enemyID].x = \_W-50        enemies[enemyID].y = mRand(50, \_H-50)   end      enemyID = enemyID + 1    end   -- SPAWN 100 ENEMIES timer.performWithDelay(1000, spawn, 100)  

 

Of course you can change this to create the enemies in the spawn method instead of creating the 100 enemies ahead of time.

 

In either case, you should have a method call that will handle cleaning up the enemy objects once they are destroyed, no longer needed, and if you want to destroy/save/pause the enemies when you leave the present storyboard scene.  But I only had time to slap together this example, just to directly answer your immediate question.  

 

As far as using within storyboard, it makes sense you would do all this in the ‘gameScene’ or ‘levelScene’ whatever you titled the main scene of your app where all this action is to occur.

 

Good luck!

 

Hope this helps you with you question.

Thanks for that post. I think I was having the biggest problem with the sprite sheets. I know how to spawn still images, but sprites don’t work the same way (at least not in my case). So looks like study time…

Thanks again for that sample. :slight_smile:

I do think they work the same as still images; but that is if you are using the newImageSheet api and not the older sprite stuff from way back when.

If you are not using the  ‘newImageSheet’ and the ‘newSprite’, you should be.  There are a few good tutorials on corona site on these.

Once you are using these, spawning and moving the sprite object is the same as a still image!!!

just do this :

– start the animation loop

enemies[1]:play()  

– move the enemy

transition.to( enemies[1], {time = 2000, x = _W/2, y = _H/2 })

This will move the enemy object from it’s random spawn location to the center of the screen

of course, it sounds like you have enough corona knowledge to figure how to transition it to whatever location 

you need to, but it really is the same as a standard display object(still image).

Of course, you can also incrementally move the enemy unit in an ‘enterFrame’ event, instead of the transition call;

but you are most likely already aware of that 

Study time is always a good thing… just don’t get stuck studying the older sprite api, it is outdated.

Good luck!