I was experimenting with the very simple code of the sample code/getting started/Frame animation 1. They have the code that is at the end of the message there:
I tried to make 2 changes to the code and see what would be the outcome. But none would work as I thought it would. First of all I deleted the line
xpos= xpos + ( xspeed * xdirection )
and then I changed the last line from
fruit:translate( xpos - fruit.x , ypos - fruit.y)
to
fruit:translate (xpos + xspeed*xdirection - fruit.x, ypos - fruit.y)
When I did that the fruit would not move at his X axis at all. This does not make sense to me. Can someone please explain the why?
Then after reopening the initial file I made one more change.
i changed the last line from
fruit:translate( xpos - fruit.x , ypos - fruit.y)
to
fruit:translate( xspped * xdirection , ypos - fruit.y)
But this seemed to create a bug as the fruit would now exit partially the screen and would not go to the left side of the screen.
Can someone please explain this too.
Thanx anyway
Charalampos
local background = display.newImage( “grass.png” )
local radius = 40
local xdirection = 1
local ydirection = 1
local xspeed = 5.5
local yspeed = 10.4
local xpos = display.contentWidth * 0.5
local ypos = display.contentHeight * 0.5
local fruit = display.newImage( “fruit.png”, xpos, ypos )
– Get current edges of visible screen (accounting for the areas cropped by “zoomEven” scaling mode in config.lua)
local screenTop = display.screenOriginY
local screenBottom = display.viewableContentHeight + display.screenOriginY
local screenLeft = display.screenOriginX
local screenRight = display.viewableContentWidth + display.screenOriginX
local function animate(event)
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
if ( xpos > screenRight - radius or xpos < screenLeft + radius ) then
xdirection = xdirection * -1;
end
if ( ypos > screenBottom - radius or ypos < screenTop + radius ) then
ydirection = ydirection * -1;
end
fruit:translate( xpos - fruit.x , ypos - fruit.y)
end [import]uid: 74929 topic_id: 12890 reply_id: 312890[/import]