0% found this document useful (0 votes)
37 views3 pages

Player Movement

This document provides code to create a simple game where a character ("peach") can be moved around the screen using arrow buttons. The code defines functions for each arrow button that set the character's motion when touched. It also includes code to continuously move the character, stop its motion, and restrict it to within the screen boundaries.

Uploaded by

Caleb Homes
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views3 pages

Player Movement

This document provides code to create a simple game where a character ("peach") can be moved around the screen using arrow buttons. The code defines functions for each arrow button that set the character's motion when touched. It also includes code to continuously move the character, stop its motion, and restrict it to within the screen boundaries.

Uploaded by

Caleb Homes
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PASTE the below code into your main.

lua file, or if you are using director, into the appropriate file. (Eg, game.lua.)
1 display.setStatusBar (display.HiddenStatusBar) 2 -- Hides the status bar 3 4 local background = display.newImage ("background.png") 5 -- Sets the background 6 7 local peach = display.newImage ("peach.png") 8 peach.x = 240 9 peach.y = 160 10 -- Puts the usual picture of me into the app and positions it 11 12 ---------------------------------------------------------------------13 local up = display.newImage ("upx.png") 14 up.x = 70 15 up.y = 230 16 17 local down = display.newImage ("downx.png") 18 down.x = 70 19 down.y = 290 20 21 local left = display.newImage ("leftx.png") 22 left.x = 30 23 left.y = 260 24 25 local right = display.newImage ("rightx.png") 26 right.x = 110 27 right.y = 260 28 29 -- Puts in all four movement arrow images and positions them 30 ---------------------------------------------------------------------31 32 local motionx = 0 33 local motiony = 0 34 local speed = 10 36

35 -- Speed can be adjusted here to easily change how fast my picture moves. Very conveni

37 local function stop (event) 38 if event.phase =="ended" then 39 40 motionx = 0 41 motiony = 0 42 end 43 end 44 45 Runtime:addEventListener("touch", stop ) 46 -- When no arrow is pushed, this will stop me from moving. 47 48 local function movepeach (event) 49 peach.x = peach.x + motionx 50 peach.y = peach.y + motiony 51 end 52 53 Runtime:addEventListener("enterFrame", movepeach) 54 -- When an arrow is pushed, this will make me move. 55 56 function up:touch() 57 motionx = 0 58 motiony = -speed 59 end 60 up:addEventListener("touch", up) 61 62 function down:touch() 63 motionx = 0 64 motiony = speed 65 end 66 down:addEventListener("touch", down) 67 68 function left:touch() 69 motionx = -speed 70 motiony = 0 71 end 72 left:addEventListener("touch",left) 73 74 function right:touch()

75 motionx = speed 76 motiony = 0 77 end 78 right:addEventListener("touch",right) 79 81 -- the way I should move based on each touch. 82 83 local function wrap (event) 84 85 if peach.x < 50 then 86 peach.x = 50 87 end 88 ---89 if peach.x > 430 then 90 peach.x = 430 91 end 92 ---93 if peach.y > 270 then 94 peach.y = 270 95 end 96 ---97 if peach.y < 50 then 98 peach.y = 50 99 end 100 ---101 if speed < 0 then 102 speed = 0 103 end 104 end 105 106 Runtime:addEventListener("enterFrame", wrap) 107

80 -- The above four functions are stating the arrows should all listen for touches and d

108 -- This will keep me from going off the sides of the screen. It's not actually a wrap, 110 -- whatever function I use to keep the main "hero" on screen "wrap", and I doubt that 111 -- time soon.

109 -- involve me disappearing and popping back on the opposite side, but I'm in the habbi

You might also like