I need help with making the actor/character stay on screen. When I tilt my device to either right or left. It disapears.
How do I solve this problem?.
I’ve been thinking about using walls. What would the code be for making walls on each side, using landscape mode. 320 x 480.
The accelerometer did only work in “potrait” but now when im using landscape it doesnt work…
-----Accelerometer
local motionx = 0
local function onAccelerate( event )
motionx = 20 * event.xGravity
end
Runtime:addEventListener (“accelerometer”, onAccelerate)
local function movecharacter (event)
character.x = character.x + motionx
end
Runtime:addEventListener(“enterFrame”, movecharacter)
Anyone have any ideas why it doesnt work in landscape mode?
I tried changing that what you said. But the ball just goes opposite if i tilt to the left, the ball goes right. Oh and the character moving off screen, still didn’t solve it <. any ideas topic_id:="" reply_id:=""></.>
also you could check the coordinates of the character by adding this to your movecharacter method:
local function movecharacter (event)
character.x = character.x - motionx
print ("character.x: "..character.x.." / character.y: "..character.y)
-- take a look what's put out in the terminal and
-- change boundaries accordingly:
local boundX = 480
local boundY = 320
if (character.x \< 0 ) then character.x = 0 end
if (character.x \> boundX) then character.x = boundX end
if (character.y \< 0 ) then character.y = 0 end
if (character.y \> boundY) then character.y = boundY end
end
I dont know if this will help you and i see you are not using physics for your player but i had the same problem when switching from portrait to landscape, here is the code i used to get my ball rolling in the correct directions, see if you can pick it and use it for what you need.
Speedy-boy, add the character check inside your enterFrame function, so it is constantly executed. Else the code will only execute it once and then not run through it again…
[code]
local function movecharacter (event)
character.x = character.x + motionx
if (character.x < 0 ) then
character.x = 0
end
if (character.x > 480) then
character.x = 480
end
end
Runtime:addEventListener(“enterFrame”, movecharacter)
[/code] [import]uid: 14018 topic_id: 24181 reply_id: 97704[/import]
The accelerometer value depends on which landscape mode (left or right) you are holding the device. If it was going in the opposite direction, then you should subtract motionx instead of adding it.
One other note, for your movecharacter function, try setting the x value based on the center of the screen minus the gravity value like this:
[lua]local function movecharacter (event)
character.x = 240 - motionx
if (character.x < 0 ) then
character.x = 0
end
if (character.x > 480) then
character.x = 480
end
end[/lua]
This will center him if the device is level and move him to the left and right depending on how much the device is tilted.
You can also adjust the sensitivity of the accelerometer by changing the value in your event like this.
This will make it less sensitive so you have to tilt a little more to move your character further. The value you are using will take very little tilt to move the entire screen.
Mark [import]uid: 117098 topic_id: 24181 reply_id: 97702[/import]