Hello,
I am having some difficulties to understand the general flow of onEnterFrame event at the moment and structure of Physics bodies. I look thru some examples and I am wondering if there is an easier way to create physics bodies. What I do now for a physics body with an image representation is:
Create a img representation
Create a physics body
create a fixture
and attach all of those together, which seems quite complicated. Am I missing something ?
-- Img representtion for physics body
local Player = Bitmap.new(Texture.new("man.png"))
Player:setAnchorPoint(0.5,0.5)
Player:setPosition(x,y)
-- Body itself
local body = self.world:createBody{type = b2.DYNAMIC_BODY}
body:setPosition(Player:getX(), Player:getY())
body:setAngle(Player:getRotation() * math.pi/180)
local poly = b2.PolygonShape.new()
-- Fixture
poly:setAsBox(Player:getWidth()/2, Player:getHeight()/2)
local fixture = body:createFixture{shape=poly, density=1.0, friction=1.0, restitution=0.8}
Player.body = body
Player.body.type = "Player"
self:addChild(Player) |
Another issue I am having is with onEnterFrame events, While I can do this very easily on AS3 or some other platform
here I can not say I am successful.
what I don't understand is do I need to move image bodies separated from physics bodies ?
Isn't there a way to attach those to each other so when I move a physics body or change something physics related
the image rep. also moves ?
And lastly can anyone show me an example of click-to-go example , can't find in examples folder.
Thanks
Comments
local player = self:player(100,100) -- Creates player object at 100,100
player:addEventListener(Event.MOUSE_DOWN, onMouseDown, player)
function onMouseDown(self, event)
print(event.x, event.y)
print(self:getX())
self:setX(event.x)
end
but setX does not work while, getX() works.
Thanks
Because you are changing x and y coordinates of a sprite and sprite is attached to physical body, in your code:
You set x coordinate for sprite and on next enterframe call it changes back where physical body was.
So if you want to move an object, you should move it's physical body. Then sprite would follow it.
Like this:
And yes you need to hook up sprites/images to physical bodies, but I see, you already done that.
And there is a global object as in AS, it's called stage. But using it would not be a best practice. It's better to choose the lesser possible scope to store variables, which in your case is scene class. So yes, I guess the way you handle variables now looks ok.
I am quite confused about using of self, it looks like the .this of AS3 but I think it is not.
Main difference is that I saved Player object, not as local object (which would be visible only inside init function), but as class property self.Player, so it would be accessible from all class methods.
Then I rewrote definition of Touch handler, so it is a class method Player:onTouch(event), thus self.Player would be accessible from it.
And the when adding touch event handler, I passed self parameter, to make it reference as a class, again so self.Player would be accessible from it.
Here are some links for basic examples to create classes, uses properties in class methods, passing data to event handler and difference between . and :
http://www.giderosmobile.com/forum/discussion/comment/6137
http://www.giderosmobile.com/forum/discussion/926/how-to-pass-variables-through-event-listeners-039attempt-to-index-a-nil-value039
http://www.giderosmobile.com/forum/discussion/comment/6033
http://www.giderosmobile.com/forum/discussion/comment/1836
Likes: twisttap
function Player:init(x,y,world)
is it possible to reach the world without passing it as a parameter ?
You could define world as global variable, but it pollutes the scope and may lead to memory issues.
You could create physical body inside scene class, after returning the instance and adding player to the scene. But this would only lead to writing more code and not reusing existing.
You could use Sprite:getParent() method to get scene, where you added player and get world through that, but again it's a complex chain of commands and not much of a code is reused.