Author Topic: Removing objects when not on screen  (Read 2019 times)

caladan21ph

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Removing objects when not on screen
« on: Tue, Jan 10, 2012 »
hello! How do I make it so that objects which are not on screen are removed from the game. I'm using flxgroups to create platforms made up of tiles. I want the platforms to disappear when it is no longer in sight because it slows down the game. Thanks!
Check out my game journal: http://alternatdev.wordpress.com

KunoNoOni

  • "Never tell me the odds"
  • Active Member
  • ***
  • Posts: 170
  • Karma: +0/-0
  • Code is Love
    • View Profile
Re: Removing objects when not on screen
« Reply #1 on: Tue, Jan 10, 2012 »
You should be able to do FlxSprite.kill(), this will remove your sprite from the game. Just replace FlxSprite with the name of your sprite.



-KunoNoOni

wg/funstorm

  • Global Moderator
  • Key Contributor
  • *****
  • Posts: 596
  • Karma: +0/-0
    • View Profile
    • Funstorm
Re: Removing objects when not on screen
« Reply #2 on: Tue, Jan 10, 2012 »
And you can use FlxSprite.onScreen() to check whether it's on-screen or not  ;)

Raf

  • Contributor
  • ****
  • Posts: 268
  • Karma: +0/-0
    • View Profile
Re: Removing objects when not on screen
« Reply #3 on: Tue, Jan 10, 2012 »
I use the exists property for that. When they move out of the screen, I set exists to false, so I can move the item back elsewhere if necessary. When checking game events, I only do so with the ones that exist.

Is that the same as the kill() method? And if not, what's the difference?

caladan21ph

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: Removing objects when not on screen
« Reply #4 on: Tue, Jan 10, 2012 »
Ok, I did it, thanks!:) I had a problem with onscreen because I created the platforms outside of the screen first before the camera can get to it. I'm trying to experiment on randomly generating levels, this one is for something like canabalt(what do you call these types of games anyway). For these kinds of games how are the platforms normally made?
Check out my game journal: http://alternatdev.wordpress.com

John Hutchinson (Johntron247)

  • Commodore 256
  • Contributor
  • ****
  • Posts: 392
  • Karma: +1/-0
  • I can has lazerz?
    • View Profile
    • Level X Games
Re: Removing objects when not on screen
« Reply #5 on: Tue, Jan 10, 2012 »
Ok, I did it, thanks!:) I had a problem with onscreen because I created the platforms outside of the screen first before the camera can get to it. I'm trying to experiment on randomly generating levels, this one is for something like canabalt(what do you call these types of games anyway). For these kinds of games how are the platforms normally made?

This kind of game is called a "Runner". I'm sure there are many ways to do it, but I'm recycling sprites.  They come into existence ahead of the player, become visible just prior to reaching the camera, and get killed when they exit the camera (ready for reuse as "new" tiles).
« Last Edit: Mon, Jan 16, 2012 by JohnTron247 »

John Hutchinson (Johntron247)

  • Commodore 256
  • Contributor
  • ****
  • Posts: 392
  • Karma: +1/-0
  • I can has lazerz?
    • View Profile
    • Level X Games
Re: Removing objects when not on screen
« Reply #6 on: Tue, Jan 10, 2012 »
I use the exists property for that. When they move out of the screen, I set exists to false, so I can move the item back elsewhere if necessary. When checking game events, I only do so with the ones that exist.

Is that the same as the kill() method? And if not, what's the difference?

Code: [Select]
public function kill():void
{
alive = false;
exists = false;
}