Need help with making actor stay on screen & Accelerometer

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?

Thanks. [import]uid: 134147 topic_id: 24181 reply_id: 324181[/import]

Hi speedy-boy,

In landscape mode, you want to look at yGravity instead of xGravity.

To keep your character from moving off screen, add this at the end of your movecharacter function:

[lua]if (character.x < 0 ) then
character.x = 0
end

if (character.x > 480) then
character.x = 480
end[/lua]

Hope that helps.

Mark [import]uid: 117098 topic_id: 24181 reply_id: 97636[/import]

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:=""></.>

try

character.x = character.x - motionx  

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  

-finefin [import]uid: 70635 topic_id: 24181 reply_id: 97669[/import]

Now it doesnt work at all… so frustrating… [import]uid: 134147 topic_id: 24181 reply_id: 97674[/import]

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.

[lua]local ham = display.newRect (0, 0, 24, 24)
ham.x = 30; ham.y = 50; ham.isBullet = true;
physics.addBody( ham, “dynamic”, { bounce = 0, friction = 0, radius = 12})

local onAccelerate = function (event)
ham:setLinearVelocity((event.yGravity * 300) * -1, (event.xGravity * 300) * -1)
end[/lua]

[import]uid: 126161 topic_id: 24181 reply_id: 97691[/import]

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]

Hi speedy-boy,

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.

[lua]local function onAccelerate( event )
motionx = 12 * event.yGravity
end[/lua]

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]