Hello, I am new to working with corona sdk, and I am using corona for my final project for my game class . At the movement, I am having some issues with the programming. My goal right now is to get the player sprite sheet/placeholder to be moved by the analog stick from this module example;
http://developer.anscamobile.com/code/simple-analog-stick-joystick-module
I have a splash screen, main menu, and then a ingame screen, the concept is a soccer game. So basically, I am trying to merge the analog stick code with the player movement as well as camera movement. The camera follows the players but I was using a D-pad which did not follow the player and went off screen.
Two things I am asking for, Can I have a analog stick control the sprite while centering the camera on it still. I tried to put the analog stick into the code but I keep getting the errors where the analog does not appear and the player cannot be moved. Second thing is if its too complicated to figure out, How do I keep the D-PAD locked in the bottom corner of my screen because when the player move across the map, the D-PAD does not move with the screen.
Here’s my whole code for my level, I had to comment some stuff out.
module(..., package.seeall)
function new()
local localGroup = display.newGroup()
localGroup.x=0
localGroup.y=0
--\> This is how we start every single file or "screen" in our folder, except for main.lua
-- and director.lua
--\> director.lua is NEVER modified, while only one line in main.lua changes, described in that file
------------------------------------------------------------------------------
------------------------------------------------------------------------------
local physics = require("physics")
local gameUI = require("gameUI")
local easingx = require("easingx")
-- Create master display group (for global "camera" scrolling effect)
--local game = display.newGroup();
--game.x = 0
local spawnLock=0
local background = display.newImage ("soccerField1.png")
--local background2 = display.newImage ("soccerField1.png")
localGroup:insert(background)
local player = display.newImage ("player.png") --Creates Player
localGroup:insert(player)
player.x = display.contentWidth / 2
player.y = display.contentHeight / 2
player.rotation = -90
local StickLib = require("lib\_analog\_stick")
-- CREATE ANALOG STICK
MyStick = StickLib.NewStick(
{
borderImage = "stick\_border.png", -- IF NOT PROVIDED, A CIRCLE SHAPE WILL BE USED
thumbImage = "stick\_thumb.png", -- IF NOT PROVIDED, A CIRCLE SHAPE WILL BE USED
x = screenW\*960,
y = screenH\*31679,
thumbSize = 28,
borderSize = 48,
snapBackSpeed = .75,
R = 255,
G = 255,
B = 255
} )
----------------------------------------------------------------
-- MAIN LOOP
----------------------------------------------------------------
local function movePlayer( event )
-- MOVE THE SHIP
MyStick:move(player, 7.0, true)
-- SHOW STICK INFO
Text.text = "ANGLE = "..MyStick:getAngle().." DISTANCE = "..math.ceil(MyStick:getDistance()).." PERCENT = "..math.ceil(MyStick:getPercent()\*100).."%"
end
Runtime:addEventListener("enterFrame", moveplayer)
--[[
local screenW = display.contentWidth
local screenH = display.contentHeight
local Text = display.newText( " ", screenW\*.6, screenH-20, native.systemFont, 15 )
-- CREATE A SPACE SHIP
local player = display.newGroup()
player.x = screenW \* .5
player.y = screenH \* .5
Tmp = display.newLine(-8,0, 8,0)
Tmp.width = 2
Tmp:setColor (255,255,255, 255)
Tmp:append (0,-24, -8,0)
player:insert (Tmp)
-- CREATE ANALOG STICK
MyStick = StickLib.NewStick(
{
borderImage = "stick\_border.png", -- IF NOT PROVIDED, A CIRCLE SHAPE WILL BE USED
thumbImage = "stick\_thumb.png", -- IF NOT PROVIDED, A CIRCLE SHAPE WILL BE USED
x = screenW\*.75,
y = screenH\*.83,
thumbSize = 28,
borderSize = 48,
snapBackSpeed = .75,
R = 255,
G = 255,
B = 255
} )
----------------------------------------------------------------
-- MAIN LOOP
----------------------------------------------------------------
local function main( event )
-- MOVE THE SHIP
MyStick:move(player, 7.0, true)
-- SHOW STICK INFO
Text.text = "ANGLE = "..MyStick:getAngle().." DISTANCE = "..math.ceil(MyStick:getDistance()).." PERCENT = "..math.ceil(MyStick:getPercent()\*100).."%"
end
Runtime:addEventListener( "enterFrame", main )
--]]
----------------------------------------------------------------------
-- ARROWS --
----------------------------------------------------------------------
--[[
local up = display.newImage ("upx.png")
localGroup:insert(up)
up.x = 230
up.y = 390
local down = display.newImage ("downx.png")
localGroup:insert(down)
down.x = 230
down.y = 450
local left = display.newImage ("leftx.png")
localGroup:insert(left)
left.x = 195
left.y = 420
local right = display.newImage ("rightx.png")
localGroup:insert(right)
right.x = 265
right.y = 420
----------------------------------------------------------------------
-- MOVE PLAYER --
----------------------------------------------------------------------
local motionx = 0
local motiony = 0
local speed = 10
-- Speed can be adjusted here to easily change how fast my picture moves. Very convenient!
local function stop (event)
if event.phase =="ended" then
motionx = 0
motiony = 0
end
end
Runtime:addEventListener("touch", stop )
-- When no arrow is pushed, this will stop me from moving.
local function moveplayer (event)
player.x = player.x + motionx
player.y = player.y + motiony
end
Runtime:addEventListener("enterFrame", moveplayer)
-- When an arrow is pushed, this will make me move.
function up:touch()
motionx = 0
motiony = -speed
end
up:addEventListener("touch", up)
function down:touch()
motionx = 0
motiony = speed
end
down:addEventListener("touch", down)
function left:touch()
motionx = -speed
motiony = 0
end
left:addEventListener("touch",left)
function right:touch()
motionx = speed
motiony = 0
end
right:addEventListener("touch",right)
-- The above four functions are stating the arrows should all listen for touches and defining
-- the way I should move based on each touch.
local function wrap (event)
if player.x \< 60 then
player.x = 60
end
----
if player.x \> 2030 then
player.x = 2030
end
----
if player.y \> display.contentHeight -20 then
player.y = display.contentHeight -20
end
----
if player.y \< 20 then
player.y = 20
end
----
if speed \< 0 then
speed = 0
end
end
Runtime:addEventListener("enterFrame", wrap)
--]]
local function moveCamera()
if (player.x \> 245 and player.x \< 3100) then
--game.x = -player.x + 80
localGroup.x = -player.x + 80
end
end
Runtime:addEventListener( "enterFrame", moveCamera )
--PLAYER PHYSICS/COLLISION
physics.start()
--physics.setDrawMode("debug") --DEBUGGING Body only visible
--physics.setDrawMode("hybrid") --DEBUGGING BOTH VISiBLE
physics.setGravity( 0, 0 )
bodyShape = { -2,-12, 4,-12, 6,35, -5,35 }
player.isBullet=true
physics.addBody( player,"static",{friction = 0.3,density=3.0, bounce = 0.8,shape=bodyShape} )
player.isSleepingAllowed=false
--[[
physics.start()
physics.setGravity( 0, 0 ) -- no gravity in any direction
--physics.addBody( goalCage, "static", { friction=0.5, bounce=0.3 } )
borderBodyElement = { friction=0.4, bounce=0.03}
borderNetBodyElement = { friction=0.4, bounce=0.005}
local borderTop = display.newRect( 100, 45, 120, 5)
borderTop:setFillColor( 0, 0, 0,0) -- make invisible
physics.addBody( borderTop, "static", borderNetBodyElement )
local borderSideLeft = display.newRect( 85, 45, 5, 60)
borderSideLeft:setFillColor( 0, 0, 0,0) -- make invisible
physics.addBody( borderSideLeft, "static", borderBodyElement )
borderSideLeft:rotate(25)
local borderSideRight = display.newRect( 230, 45, 5, 60)
borderSideRight:setFillColor( 0,0, 0, 0) -- make invisible
physics.addBody( borderSideRight, "static", borderBodyElement )
borderSideRight:rotate(-25)
--]]
-----------------------------------------------------------------
popSound = gameUI.newEventSoundXP{ ios="pop1\_caf.caf", android="pop1\_ogg.ogg" }
--labelFont = gameUI.newFontXP{ ios="Zapfino", android=native.systemFont }
--system.activate( "multitouch" )
--local diskGfx = {sBall, sBall, sBall}
local allItems = {} -- empty table for storing objects
-- Automatic culling of offscreen objects
--[[
local function removeOffscreenItems()
--spawnLock=0; --IM NOT WORKING :(
for i = 1, #allItems do
local sBall = allItems[i]
if (sBall.x) then
if sBall.x \< -100 or sBall.x \> display.contentWidth + 100 or sBall.y \< -100 or sBall.y \> display.contentHeight + 100 then
sBall:removeSelf()
end
end
end
end
--]]
local function dragBody( event )
gameUI.dragBody( event )
-- Substitute one of these lines for the line above to see what happens!
--gameUI.dragBody( event, { maxForce=400, frequency=5, dampingRatio=0.2 } ) -- slow, elastic dragging
--gameUI.dragBody( event, { maxForce=20000, frequency=1000, dampingRatio=1.0, center=true } ) -- very tight dragging, snaps to object center
end
local function spawnDisk( event )
local phase = event.phase
if(spawnLock==0) then
-- if event.phase=="ended" then
if "ended" == phase then
--media.playEventSound( popSound )
media.playEventSound( "pop1\_ogg.ogg" )
--randImage = diskGfx[math.random( 1, 3 )]
--allDisks[#allDisks + 1] = display.newImage( randImage )
-- localGroup:insert(randImage)
--local disk = allDisks[#allDisks]
local sBall = display.newImage("soccerBall.png")
localGroup:insert(sBall)
--sBall: toBack()
--sBall.x = event.x; sBall.y = event.y
sBall.x=display.contentWidth/2; sBall.y=display.contentHeight/2 -100
sBall.rotation = math.random( 1, 360 )
sBall.xScale = 0.05; sBall.yScale = 0.05
transition.to(sBall, { time = 500, xScale = 0.1, yScale = 0.1, transition = easingx.easeOutElastic }) -- "pop" animation
physics.addBody( sBall, { bounce=0.8, friction=0.3, radius=11.0} )
sBall.linearDamping = 0.4
sBall.angularDamping = 0.6
sBall:addEventListener( "touch", dragBody ) -- make object draggable
spawnLock=1; --Disables Repeat Spawning
end
end
end
background:addEventListener( "touch", spawnDisk ) -- touch the screen to create disks
--Runtime:addEventListener( "enterFrame", removeOffscreenItems ) -- clean up offscreen disks
--[[
local function coordinateChecker(event)
print("X:" ,event.x, "Y:", event.y)
end
background:addEventListener( "touch", coordinateChecker )
--]]
------------------------------------------------------------------------------
------------------------------------------------------------------------------
return localGroup
end
MAIN.LUA
--\> Makes a timed splash screen
local easingx = require("easingx")
--\> Makes a timed splash screen
splashBkg = display.newImage("splashBkg.png")
splash = display.newImage("splash.png", display.contentWidth/2 -280,display.contentHeight/2 -150)
local w,h = display.contentWidth, display.contentHeight
-------------------------------
-- Create master display group (for global "camera" scrolling effect)
--local red = display.newGroup();
--red.x = 0
-------------------------------
splash.xScale = 0.05; splash.yScale = 0.05
transition.to(splash, { time = 1500, xScale = 0.45, yScale = 0.45, transition = easingx.easeOutElastic })
transition.to( splash, { time=1000, delay=3500, alpha=0} )
transition.to( splashBkg, { time=1000, delay=3500, alpha=0} )
display.setStatusBar (display.HiddenStatusBar)
--\> Hides the status bar
--\> Delayed startup of app
timer.performWithDelay(4000,main)
--main()
--\> Starts our app
display.setStatusBar (display.HiddenStatusBar)
--\> Hides the status bar
local director = require ("director")
--\> Imports director
local mainGroup = display.newGroup()
--\> Creates a main group
local function main()
splash:removeSelf()
splash=nil
splashBkg:removeSelf()
splashBkg=nil
--\> Adds main function
mainGroup:insert(director.directorView)
--\> Adds the group from director
director:changeScene("menu")
--\> Change the scene, no effects
return true
end
--\> Delayed startup of app
timer.performWithDelay(1000,main)
--Original delay = 5000
--main()
--\> Starts our app
[import]uid: 96127 topic_id: 17512 reply_id: 317512[/import]