Why can't I disable gravity from PlayScreen.onResetEvent() instead of game.loaded()

66 views
Skip to first unread message

Jordan Applewhite

unread,
Sep 23, 2015, 4:08:22 PM9/23/15
to melonJS - A lightweight HTML5 game engine
Hello! I've just started using MelonJS and I'm new to Javascript in general. I have worked on a few sponsored flash games in the past using Flashpunk and AS3 so I'm familiar with core 2d gamedev concepts. 

I saw in the API docs that I can use 
me.sys.gravity = false;
to disable gravity for games like RPGs and Shooters (currently working on an RPG). 

Disabling gravity this way works if I call in game.loaded(), but it doesn't work if called from PlayScreen.onResetEvent() (which inherits from ScreenObject). As I understand it, a ScreenObject sets up gameplay while a game container exists to manage switching between different ScreenObjects. So shouldn't I be able to change global properties such as me.sys.gravity from within PlayScreen.onResetEvent()? It seems like that's where other gameplay-defining events happen such as adding entities. 

Thanks for your help! I'll probably be asking a bunch of questions as I work through this game. 

Aaron McLeod

unread,
Sep 23, 2015, 4:19:39 PM9/23/15
to melonJS - A lightweight HTML5 game engine
You should be able to, so long as you're doing it before loading any maps or objects into the game. The physics body checks the system property in the constructor. If you do have the setting where you think it should be, post your code and I'll have a further look.

Thanks!

Jordan Applewhite

unread,
Sep 23, 2015, 4:23:08 PM9/23/15
to mel...@googlegroups.com
I have basically the same question for why I can't do 
me.pool.register("mainPlayer", game.PlayerEntity);

from within PlayScreen.onResetEvent(). It would make sense that you could because that's where the HUD is being added. And I imagine other entities would be added there as well. 

Sorry if I'm being dense! I'm sure there is some obvious reasoning I haven't figured out yet. 

Jordan Applewhite

unread,
Sep 23, 2015, 4:34:58 PM9/23/15
to mel...@googlegroups.com
Yup, that was it. I moved: 
me.sys.gravity = false;
me.pool.register("mainPlayer", game.PlayerEntity);

before the call to: 
me.levelDirector.loadLevel("desert");

and it worked :)

Much appreciated! Thanks Aaron!

--
You received this message because you are subscribed to a topic in the Google Groups "melonJS - A lightweight HTML5 game engine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/melonjs/bu1lsyOTI-E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to melonjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jay Oster

unread,
Sep 23, 2015, 4:37:07 PM9/23/15
to mel...@googlegroups.com
Hi Jordan,

There's nothing inherently preventing you from configuring settings, or registering classes with the pool at any time. As Aaron mentioned, you will want to do things in a specific order, regardless of timing in relation to other events.

Registering a class with the pool is typically done at the start of the game, before switching to the first state. This is just done as a matter of convenience; it's part of the game's initialization, not part of the play screen's initialization.

For some general background information: The object lifecycle within melonJS provides a few different events which are triggered at different stages of the lifecycle. For example, init is used as the object constructor, and is only called once for the entire lifetime of the object. onResetEvent (aka onActivateEvent) is triggered every time the object is added to the scene graph, and onDestroyEvent is triggered when it is removed. This provides events for "soft" initialization and destruction. E.g. for resetting properties on the object after it has been recycled into the pool.

A less complex approach is to just not use the pool; let JavaScript run its Garbage Collection to free memory automatically when objects are removed from the scene. This will incur a performance hit, however. And some objects like me.ScreenObject are never released to GC...

--
You received this message because you are subscribed to the Google Groups "melonJS - A lightweight HTML5 game engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to melonjs+u...@googlegroups.com.

Jordan Applewhite

unread,
Sep 23, 2015, 5:06:00 PM9/23/15
to mel...@googlegroups.com
Thanks, Jay. Great info and I appreciate the quick response. I'm still trying to learn how to instantiate entities manually rather than using the pool as it seems there is some automagic happening behind the scenes between objects in the Tiled map and the function me.levelDirector.loadLevel();  I used to do all that manually in Flashpunk so I'm still figuring out how that applies here. Is me.game.world the scene graph? Would I do something like:
game.player = new PlayerEntity(); //with PlayerEntity.init() filled out to pass in x, y, dimensions, and resources
me.game.world.addChild(game.player);

I still haven't worked through the SpaceInvaders tutorial, maybe I am missing some critical info from there. Thanks again :)

Aaron McLeod

unread,
Sep 23, 2015, 5:32:35 PM9/23/15
to melonJS - A lightweight HTML5 game engine
I specifically wrote the space invaders tutorial to not use tiled or the level director, so users like yourself could learn how to manage things in the game stage :).

I don't recall if it uses object pooling or not. Honestly it's a good idea. me.pool.pull is basically a shortcut for new MyEntity(args), but it will re-use an existing object if one exists. This reduces problems with garbage collection cycles. Learning to leverage the life cycle methods that Jason mentioned can help making pooling really efficient.
To unsubscribe from this group and all its topics, send an email to melonjs+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "melonJS - A lightweight HTML5 game engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to melonjs+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "melonJS - A lightweight HTML5 game engine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/melonjs/bu1lsyOTI-E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to melonjs+unsubscribe@googlegroups.com.

Jordan Applewhite

unread,
Sep 23, 2015, 6:02:18 PM9/23/15
to mel...@googlegroups.com
Perfect! I'll check that tutorial later tonight :) 

To unsubscribe from this group and all its topics, send an email to melonjs+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "melonJS - A lightweight HTML5 game engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to melonjs+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "melonJS - A lightweight HTML5 game engine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/melonjs/bu1lsyOTI-E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to melonjs+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "melonJS - A lightweight HTML5 game engine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/melonjs/bu1lsyOTI-E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to melonjs+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages