Okay, attached is a basic player movement template I am trying to make. It has a scene (a physics bounding box), a box and 2 arrows. While working on this I want to ask questions about the problems I came by.
1- I saw in some examples that physics world iterations done inside the moving object self ( in Jumping game template the physics world iterations done inside the player itself) that may be okay for 1 object maybe, but what if I have 30 objects on stage. Isn't it good idea to have a general physics iteration that goes over all objects ?
2- In the file I attached, I run the iterations on main.lua. The Players physical body works fine but the Graphical representation to the player body is not attached to physical body, whats the fix for this ?
3- Also in the jumping template I saw that Player controls are embedded inside the again Player.lua, however I want to
make control pads as an another class and Player independent. And even tho I tried something like that, I can not message between Player Class <-> Buttons Class which both added to main.lua during runtime.
4-Also I can not find enough examples for physical velocity and force usage. (Is there a better way to search Gideros Forums ?)
5-I think it might be a good idea to demonstrate an advanced example of OOP basics like messaging between different Objects, inheritance, encapsulation etc.
6-I saw some suggestions on Forum that making a community game all together. I think before stepping to that, maybe we should all together go over the general documentation of Gideros API. We can decide on a standard for API and make advanced and better explanations with examples for the API or we can update the Ultimate Guide !
Thank you very much for your time.
Comments
Also for the code below when I declare body as a local variable then try to attach, it does not work. But when I do it as self.body it works, I think both should work here because both of them in the same class and in the same function.
So I'll try explain
1. Yes it is probably the best to have global iteration in game logic class, that handles many physical objects.
2. So you have a Sprite object called scene and inside onEnterFrame event you take all it's children and iterate through them. If child has a body property, then it's a reference to physical body and we use it.
But let's look at Player class. Player class itself is a Sprite object. Then you create a Bitmap object (which has body property with physical body) and add it to the player sprite. Then in Player class constructor you try to return that Bitmap object, but since it is a constructor all it returns is the instance of Player class, (probably overriding anything you try to return). So inside scene class you receive Sprite object which have Bitmap object with body as a child. And you add this Player sprite object to scene. So Bitmap object with physical body is not on the first level of all children.
When you iterate through scene children, all you get is a Player Sprite object, which does not have body property, but as a child has Bitmap object. (Hopefully it makes sense)
3. Well in my point of view, you have all those different classes as Player, controls, etc and one main game logic class, which is also a scene, where you handle all this objects.
And as suggestion, you can try using GregBug's http://www.tntparticlesengine.com/?cat=13
4. Better forum search, might be site:giderosmobile.com/forum box2d in google
But you can also check here for examples: http://appcodingeasy.com/Gideros-Mobile
Not specifically force and impulses, but they are used as to demonstrate different combinations of effects, as for example, magnets
thank you
I would suggest you don't name local variables the same as your classes or you may be running into a lot of trouble due to your using a local variable named Player inside a class named Player, which means you are liking either overwriting the data or at the least misreferencing things.
@ar2rsawseen is of course right here, you should go ahead and assign the body to your Player Obj, instead of directly to the child sprite, so when the loop goes through it can access the body and move the whole player object--and it's child the sprite.
Some thing like this:
Btw I have an another idea which I didn't tried yet but, can I make a function inside all the assets I have and inside the main loop I will make just one world step call and inside that call other Objects update functions ? Is that useable ? I bought at least I will make just 1 onEnterframe event which seems cool
But I hope now you understand what I meant and most importantly why it works.
Creating an update position function for each object also solves the problem. But then you must maintain that every new object will have that method, or check if that method exists.
You can go either way, it's totally up to you, lua is pretty flexible
And thanks I got the idea, actually I got your idea before but I just can't find enough documentation There are lots of examples and stuff over here and giderostutorials.com for very cool subjects but I think in my humble opinion some nuances are missing that make big difference.