How to disable the functions activities of a module?

Hello ,

How to disable the functions activities of a module?

Please help me, thank you.

Can you be more specific?

assign nil to the function name, but it is unclear to me why you need to do this

for example, if you wanted to kill display.newText

display.newText = nil

I would never do this, but that would get rid of it forever after that.

Hello again ,

I explain more.

I have a module for producing enemies. On the gameover page, I want to destroy all the activities in the play section, so that the game is completely stopped; the enemies will be removed from the page and no other enemies will be produced.

Please help me, thanks.

I’m afraid what you’re asking for a way too specific to your implementation of a game to give a working example.

However, you have at least two basic approaches to ‘spawning over time’

  1. Using a timer

    local spawnTimer = timer.performWithDelay( 500, spawnEnemy, -1 )

… then later, you can cancel the timer to ‘stop’ it, as long as ‘spawnTimer’ the variable is in scope

timer.cancel(spawnTimer)
  1. Using enterFrame event

    local lastSpawnTime = system.getTimer() local tweenSpawnTime = 5555 local function onEnterFrame() local curTime = system.getTimer() if( curTime - lastSpawnTime < tweenSpawnTime ) then return end lastSpawnTime = curTime spawnEnemy() end Runtime:addEventListener(“enterFrame”, onEnterFrame)

… then later, you can cancel the the enterFrame listener to ‘stop’ it, as long as ‘onEnterFrame’ the function is in scope

Runtime:removeEventListener( "enterFrame", onEnterFrame )

As far as how you would do this in a module… well if you don’t know that already, you should take a time out and learn about modules and scope within and between them.   Then come back to this problem and try again.

One last bit of advice I would organize my module something like this:

local public = {} local spawnTimer function public.spawnEnemy() end function public.startSpawner() spawnTimer = timer.performWithDelay( 500, public.spawnEnemy, -1 ) end function public.stopSpawner() if( not spawnTimer ) then return end timer.cancel(spawnTimer) spawnTimer = nil end return public

Clearly, there are a lot of blanks to fill in there, but that is a starting point  (with the prior code).

Hello and thanks for help ,

No, I’m creating a medium shooting game…

But the problem is not just that!

I want the enemies to be removed from the page; they will not be!

Thank you.

Are you using Composer? This is a pretty key piece of information - if you are, just add all enemies to the scene.view group, and when you call the gameOver scene, they will be deleted automatically.

If not, you’ll have to remove the display group they belong to, or put them in a table and delete each of them individually, in both cases using display.remove(yourEnemyObject). 

You have been given some great help above but to keep things simple…

Everything your “enemy module” does will have to be undone to remove the enemies on screen.  You will need to do this in reverse.

So you will need to stop any active transitions, remove any enterframes you may have, stop any timers and then finally remove the display objects.

If you don’t do this procedure you will potentially get lots of errors and crashes.

You can also use the finalize event to help you clean up Runtime events listeners attached to objects.

The finalize event will be called just before the object is removed, then you can do cleanup as SGS is suggesting.

Hello and thank you for help ,

I make a group and insert all enemies to it. Then I deleted it when started the gameover scene. Also, I deleted all the timers; The problem was solved. (Of course I tried with scene.view; it was not.)

Good luck

Can you be more specific?

assign nil to the function name, but it is unclear to me why you need to do this

for example, if you wanted to kill display.newText

display.newText = nil

I would never do this, but that would get rid of it forever after that.

Hello again ,

I explain more.

I have a module for producing enemies. On the gameover page, I want to destroy all the activities in the play section, so that the game is completely stopped; the enemies will be removed from the page and no other enemies will be produced.

Please help me, thanks.

I’m afraid what you’re asking for a way too specific to your implementation of a game to give a working example.

However, you have at least two basic approaches to ‘spawning over time’

  1. Using a timer

    local spawnTimer = timer.performWithDelay( 500, spawnEnemy, -1 )

… then later, you can cancel the timer to ‘stop’ it, as long as ‘spawnTimer’ the variable is in scope

timer.cancel(spawnTimer)
  1. Using enterFrame event

    local lastSpawnTime = system.getTimer() local tweenSpawnTime = 5555 local function onEnterFrame() local curTime = system.getTimer() if( curTime - lastSpawnTime < tweenSpawnTime ) then return end lastSpawnTime = curTime spawnEnemy() end Runtime:addEventListener(“enterFrame”, onEnterFrame)

… then later, you can cancel the the enterFrame listener to ‘stop’ it, as long as ‘onEnterFrame’ the function is in scope

Runtime:removeEventListener( "enterFrame", onEnterFrame )

As far as how you would do this in a module… well if you don’t know that already, you should take a time out and learn about modules and scope within and between them.   Then come back to this problem and try again.

One last bit of advice I would organize my module something like this:

local public = {} local spawnTimer function public.spawnEnemy() end function public.startSpawner() spawnTimer = timer.performWithDelay( 500, public.spawnEnemy, -1 ) end function public.stopSpawner() if( not spawnTimer ) then return end timer.cancel(spawnTimer) spawnTimer = nil end return public

Clearly, there are a lot of blanks to fill in there, but that is a starting point  (with the prior code).

Hello and thanks for help ,

No, I’m creating a medium shooting game…

But the problem is not just that!

I want the enemies to be removed from the page; they will not be!

Thank you.

Are you using Composer? This is a pretty key piece of information - if you are, just add all enemies to the scene.view group, and when you call the gameOver scene, they will be deleted automatically.

If not, you’ll have to remove the display group they belong to, or put them in a table and delete each of them individually, in both cases using display.remove(yourEnemyObject). 

You have been given some great help above but to keep things simple…

Everything your “enemy module” does will have to be undone to remove the enemies on screen.  You will need to do this in reverse.

So you will need to stop any active transitions, remove any enterframes you may have, stop any timers and then finally remove the display objects.

If you don’t do this procedure you will potentially get lots of errors and crashes.

You can also use the finalize event to help you clean up Runtime events listeners attached to objects.

The finalize event will be called just before the object is removed, then you can do cleanup as SGS is suggesting.