Hi, I’ve used spritehelper and levelhelper to put together my level -
the player is comprised of two sprites with seperate animation which are joined by a Distance joint (the idea is that the animations change when you press a button - shoot - the top half changes anim - move joystick and the bottom half changes anim).
In levelhelper i created the sprite and everything loads up well.
in my code i added this -
local player = loader:spriteWithUniqueName("player")
and then added the dpad code
--Creates Buttons and Various Interface Images local button = display.newImage ("images/button.png",50,300) local button2 = display.newImage ("images/button.png",50,375) local button3 = display.newImage ("images/button.png",50,375) local button4 = display.newImage ("images/button.png",50,375) local button5 = display.newImage ("images/buttonv2.png",50,375) local button6 = display.newImage ("images/buttonv2.png",50,375) local button7 = display.newImage ("images/buttonv2.png",50,375) local button8 = display.newImage ("images/buttonv2.png",50,375) local buttonplus = display.newImage ("images/plus.png") local buttonminus = display.newImage ("images/minus.png") local backbutton = display.newImage ("images/menu.png") local text = display.newText("Speed",50,50,nil,26) text:setTextColor(255,255,255) --Sets up were all the objects should go and other properties button.x = display.contentWidth / 2 button.y = display.contentHeight - 212 button.alpha = 0.5 -- button2.x = display.contentWidth / 2 -- X Pos. button2.y = display.contentHeight - 100 --Y Pos. button2.alpha = 0.5 -- Alpha Value button2.rotation = 180 --Rotaion -- button3.x = (display.contentWidth / 2) - 56 button3.y = display.contentHeight - 156 button3.alpha = 0.5 button3.rotation = -90 -- button4.x = (display.contentWidth / 2) + 56 button4.y = display.contentHeight - 156 button4.alpha = 0.5 button4.rotation = 90 -- button5.x = (display.contentWidth / 2) + 56 button5.y = display.contentHeight - 100 button5.alpha = 0.5 button5.rotation = 90 -- button6.x = (display.contentWidth / 2) + 56 button6.y = display.contentHeight - 212 button6.alpha = 0.5 button7.x = (display.contentWidth / 2) - 56 button7.y = display.contentHeight - 212 button7.alpha = 0.5 button7.rotation = -90 button8.x = (display.contentWidth / 2) - 56 button8.y = display.contentHeight - 100 button8.alpha = 0.5 button8.rotation = 180 player.x = display.contentWidth / 2 player.y = display.contentHeight - 300 buttonplus.x = (display.contentWidth / 2) - 125 buttonplus.y = display.contentHeight - 25 buttonplus.alpha = 0.5 buttonminus.x = (display.contentWidth / 2) + 125 buttonminus.y = display.contentHeight - 25 buttonminus.alpha = 0.5 backbutton.alpha = 0.5 text.x = (display.contentWidth / 2) text.y = display.contentHeight - 25 ---- --Variables that will be used local motionx = 0 --X Motion local motiony = 0 -- Y Motion local speed = 5 -- Speed local function stop (event) --Statements to control when there is no --touch and the touch is outside the D-Pad if "ended" == event.phase then --Check to see if the touch ended motionx = 0 motiony = 0 end --Touch Pos. Checkers if event.y \< (display.contentHeight - 252) then motionx = 0 motiony = 0 end if event.y \> (display.contentHeight - 70) then motionx = 0 motiony = 0 end if event.x \> (display.contentWidth - 70) then motionx = 0 motiony = 0 end if event.x \< 69 then motionx = 0 motiony = 0 end end Runtime:addEventListener("touch", stop ) --What activates the stop function ---- local function move (event) -- Moves and updates the Player's movement --and the speed feedback player.x = player.x + motionx player.y = player.y + motiony text.text = speed end Runtime:addEventListener("enterFrame", move) --Every frame of the program will --run this function ----- local function wrap (event) --Function used to keep the player on screen if player.x \< -85 then player.x = 405 end ---- if player.x \> 405 then player.x = -85 end ------- if player.y \< -88 then player.y = 568 end ---- if player.y \> 568 then player.y = -88 end --- if speed \< 0 then speed = 0 end end Runtime:addEventListener("enterFrame", wrap) ------- --Button Functions to set the motionx and motiony variables function button:touch() motionx = 0 motiony = -speed end button:addEventListener("touch", button) ------ function button2:touch() motionx = 0 motiony = speed end button2:addEventListener("touch", button2) ------ function button3:touch() motionx = -speed motiony = 0 end button3:addEventListener("touch", button3) ------ function button4:touch() motionx = speed motiony = 0 end button4:addEventListener("touch", button4) ------- ----- function button5:touch() motionx = speed motiony = speed end button5:addEventListener("touch", button5) function button6:touch() motionx = speed motiony = -speed end button6:addEventListener("touch", button6) function button7:touch() motionx = -speed motiony = -speed end button7:addEventListener("touch", button7) function button8:touch() motionx = -speed motiony = speed end button8:addEventListener("touch", button8) --Buttons to set the speed of the players movement function buttonplus:tap() speed = speed + 1 end buttonplus:addEventListener("tap", buttonplus) function buttonminus:tap() if speed \>= 1 then speed = speed - 1 end end buttonminus:addEventListener("tap", buttonminus) function backbutton:tap() --Button that calls the director to change the scene/window Runtime:removeEventListener( "enterFrame", wrap ) --Removes the event listener Runtime:removeEventListener( "enterFrame", move ) --Removes the event listener director:changeScene("menu","moveFromLeft") --Changes the scene end backbutton:addEventListener("tap", backbutton)
I thought that this would move the bottom half of the sprite (and along with it the top half) however I get a crash with code -
File: …vonblumenthal/Desktop/TEST CODE/sprite test/main.lua
Line: 92
Attempt to index local ‘player’ (a nil value)
stack traceback:
[C]: ?
…vonblumenthal/Desktop/TEST CODE/sprite test/main.lua:92: in main chunk
i.e. when the player variable is first called by the joystick code. I can’t figure this out!