Hello everybody!
I was searching in the forum for a answer, but I didn't find it, so...
I'm trying to make a character move to one side of screen to the other using the gyroscope, but I'm not able to find the right code to do this.
I'm newbie, sorry about the stupid question
Thank you!
Comments
Likes: victor_cappa
I'm trying to make a doodle jump kind of game (learning purposes).
And here is also a Doodle Jump template:
http://giderosmobile.com/forum/discussion/473/let039s-jump-d/p1
Likes: victor_cappa
You were really helpful, thank you again!
"character.lua:50: attempt to index global 'self' (a nil value)
stack traceback:
character.lua:50: in main chunk"
here is the whole code:
require "box2d"
b2World = b2.World.new(0, 15, true);
GCchar = gideros.class(Bitmap)
function GCchar:init()
self:setAnchorPoint(0.5, 0.5);
self.body = b2World:createBody{type = b2.DYNAMIC_BODY,
};
self:setPosition(160, 492);
self.body:setPosition(self:getPosition());
local blockShape = b2.PolygonShape.new();
blockShape:setAsBox(self:getWidth()*0.5, self:getHeight()*0.5);
self.body:createFixture{shape = blockShape,
friction = 0,
restitution = 0,
density = 5,
isSensor = false
};
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self);
b2World:addEventListener(Event.BEGIN_CONTACT, self.onBeginCollision, self);
self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self);
self.body:setLinearVelocity(0, -15);
end
function GCchar:onEnterFrame()
b2World:step(1/60, 8, 3);
self:getParent():addChild(self);
self:setPosition(self.body:getPosition());
local selfX, selfY = self:getParent():localToGlobal(self:getPosition());
if selfY < 250 then
self:getParent():setY(250 -self:getY());
end
if selfX < 0 then -- para o char poder ir da esquerda para a direita e vice versa
local bodyX, bodyY = self.body:getPosition();
self.body:setPosition(320, bodyY);
elseif selfX > 320 then
local bodyX, bodyY = self.body:getPosition();
self.body:setPosition(0, bodyY);
end
end
------------------------include accelerometer------------------------
require "accelerometer"
accelerometer:start()
self.filter = 0.5
self.fx = 0
self.gravityScale = 10
-- get accelerometer values
local x, y = accelerometer:getAcceleration()
-- apply IIR filter
self.fx = x * self.filter + self.x * (1 - self.filter)
self.world:setGravity(self.fx*self.gravityScale)
function GCchar:onEnterFrame(event)
local xSpeed, ySpeed = self.body:getLinearVelocity();
if event.x < 160 then
self.body:setLinearVelocity(-8, ySpeed);
else
self.body:setLinearVelocity(8, ySpeed);
end
event:stopPropagation();
end
function GCchar:onMouseUp(event)
local xSpeed, ySpeed = self.body:getLinearVelocity();
self.body:setLinearVelocity(0, ySpeed);
event:stopPropagation();
end
function GCchar:onBeginCollision(event) -- colisão
local bodyA, bodyB = event.fixtureA:getBody(), event.fixtureB:getBody();
if bodyA == self.body or bodyB == self.body then
local xSpeed, ySpeed = self.body:getLinearVelocity();
if ySpeed > 0 then
self.body:setLinearVelocity(xSpeed, -15);
end
end
end
character.lua:52: attempt to perform arithmetic on field 'x' (a nil value)
stack traceback:
character.lua:52: in function
I include de acceleromater code into function GCchar:onEnterFrame()
require "box2d"
b2World = b2.World.new(0, 15, true);
GCchar = gideros.class(Bitmap)
function GCchar:init()
self:setAnchorPoint(0.5, 0.5);
self.body = b2World:createBody{type = b2.DYNAMIC_BODY,
};
self:setPosition(160, 492);
self.body:setPosition(self:getPosition());
local blockShape = b2.PolygonShape.new();
blockShape:setAsBox(self:getWidth()*0.5, self:getHeight()*0.5);
self.body:createFixture{shape = blockShape,
friction = 0,
restitution = 0,
density = 10,
isSensor = false
};
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self);
b2World:addEventListener(Event.BEGIN_CONTACT, self.onBeginCollision, self);
self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self);
self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self);
self.body:setLinearVelocity(0, -13);
end
function GCchar:onEnterFrame()
b2World:step(1/60, 8, 3);
self:getParent():addChild(self);
self:setPosition(self.body:getPosition());
local selfX, selfY = self:getParent():localToGlobal(self:getPosition());
if selfY < 240 then
self:getParent():setY(240-self:getY());
end
if selfX < 0 then
local bodyX, bodyY = self.body:getPosition();
self.body:setPosition(320, bodyY);
elseif selfX > 320 then
local bodyX, bodyY = self.body:getPosition();
self.body:setPosition(0, bodyY);
end
------------------------include accelerometer------------------------
require "accelerometer"
accelerometer:start()
self.filter = 0.5
self.fx = 0
self.gravityScale = 10
-- get accelerometer values
local x, y = accelerometer:getAcceleration()
-- apply IIR filter
self.fx = x * self.filter + self.x * (1 - self.filter)
self.world:setGravity(self.fx*self.gravityScale)
end
function GCchar:onMouseDown(event)
local xSpeed, ySpeed = self.body:getLinearVelocity();
if event.x < 160 then
self.body:setLinearVelocity(-4, ySpeed);
else
self.body:setLinearVelocity(4, ySpeed);
end
event:stopPropagation();
end
function GCchar:onMouseUp(event)
local xSpeed, ySpeed = self.body:getLinearVelocity();
self.body:setLinearVelocity(0, ySpeed);
event:stopPropagation();
end
function GCchar:onBeginCollision(event)
local bodyA, bodyB = event.fixtureA:getBody(), event.fixtureB:getBody();
if bodyA == self.body or bodyB == self.body then
local xSpeed, ySpeed = self.body:getLinearVelocity();
if ySpeed > 0 then
self.body:setLinearVelocity(xSpeed, -13);
end
end
end
I guess I need to study more theory before study 'real codes'...
But now it says that 'character.lua:52: attempt to index field 'world' (a nil value)
stack traceback:
character.lua:52: in function '
about this line of the code
self.fx = x * self.filter + self.fx * (1 - self.filter)
self.world:setGravity(self.fx*self.gravityScale)
Now I'm trying to figure out how to only use x axis... When I take the
self.fy*self.gravityScale
from here
b2World:setGravity(self.fx*self.gravityScale, - self.fy*self.gravityScale)
I get an error "character.lua:45: bad argument #2 to 'setGravity' (number expected, got no value)"
But it was really helpful to my learnig process
Likes: victor_cappa