How do I get my Player to display over the background? - lua

local physics = require( "physics" ) --Requires the Corona sdk physics engine
physics.start()
--Background
local background = display.newImage("Background.png")
background:translate(220,150)
--background:toBack();
--screenGroup:insert(background)
--Ground
local ground = display.newImage("GROUND1.png", 220, 300 )
physics.addbody(ground,"static",{density=1.6, friction=0.5, bounce=0.2} )
--ground:toBack(); (possibility)
--ground.x = (220)
--ground.y = (300)
--Player
local player = display.newImage("Player.png")
player:translate(200,200)
--physics.addbody(player,{})
player:toFront();
This is my lua code, the background displays, the ground displays, but the player is hidden behind the background. Can anyone explain why? Thanks in advance

When you insert group elements, the order you do it renders on screen. Try this:
local background = display.newImage("Background.png")
background:translate(220,150)
local ground = display.newImage("GROUND1.png", 220, 300 )
local player = display.newImage("Player.png")
player:translate(200,200)
screenGroup:insert(background)
screenGroup:insert(player)
--the rest of your functions or validations...
That way player will be in front of the background

Related

Physics Bug in Corona Sdk

I have a problem in Corona SDK. The physics are screwed up. Objects move in random directions
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
-- Your code here
local physics_engine = require("physics")
physics_engine.start()
local background=display.newGroup()
local main_area=display.newGroup()
local sheetOptions =
{--animated sheet options
width = 389,
height = 391,
numFrames = 10
}
--load animated image sheet
local cute_charac_sheet = graphics.newImageSheet( "spritesheet.png", sheetOptions )
local sequences_cute_charac = {
-- consecutive frames sequence
{--animation options for blinking
name = "normalRun",
start = 1,
count = 10,
time=800,
loopCount = 0,
loopDirection = "bounce"
}
}
local function woodmaker(event)
local wood =display.newImageRect(main_area,'wood.png',15,45)
wood.x=event.x
wood.y=event.y
physics_engine.addBody(wood,'kinematic')
end
local cute_charac = display.newSprite( cute_charac_sheet,sequences_cute_charac)
main_area:insert(cute_charac)
cute_charac.x=display.contentCenterX
cute_charac.y=192.55
cute_charac:scale(0.1,0.1)
cute_charac:play()
local back_png= display.newImageRect(background,"Day_Back.png",480,270)
back_png.x=display.contentCenterX
back_png.y=display.contentCenterY
back_png:addEventListener( "tap", woodmaker )
local ground_grass=display.newImageRect(main_area,"grass.png",480,60)
ground_grass.x=display.contentCenterX
ground_grass.y=240
local grass_texture=display.newImageRect(main_area,'grassup.png',480,5)
grass_texture.x=display.contentCenterX
grass_texture.y=210
physics_engine.addBody(cute_charac,"dynamic")
physics_engine.addBody(grass_texture,'static',{bounce=0,})
Also removing the ground_grass fixes it.Why is this happening
Also making the wood dynamic pushes it towards the sides and if the wood is placed in the middle it moves down.

Corona SDK - Shooting Projectiles ( Handling each instance )

I am currently trying to work out how I should go about shooting projectiles using Corona SDK. However, I don't know the best way to go about doing this. I am guessing that you have to spawn instances of the same object and apply force to them but I don't know the best way to do it or how I should handle each instance. I am still learning Lua and just need some guidance on how to do it, any help will be appreciated.
I want to be able to check if any of the bullets hit a sensor object ( I haven't implemented this but I know how to ) at the top of the screen and then destroy the bullet that hit the sensor but how do I check each instance and destroy them individually?
This is the basic structure that I have so far.
display.setStatusBar( display.HiddenStatusBar )
local physics = require( 'physics' )
physics.start()
local speed = -500
local contentW, contentH = display.contentWidth, display.contentHeight
-- Background
local bg = display.newRect( 0, 0, contentW, contentH )
bg.anchorX = 0
bg.anchorY = 0
bg:setFillColor( 0, 1, 1 )
-- Ground
local ground = display.newRect( 0, contentH - 50, contentW, 50 )
ground.anchorX = 0
ground.anchorY = 0
ground:setFillColor( 0, 0.8, 0 )
-- Hero
local hero = display.newRect( contentW / 2, contentH / 2, 40, 40 )
hero:setFillColor( 1, 0, 0 )
function shoot( event )
if ( event.phase == 'began' ) then
local projectile = display.newRect( hero.x, hero.y, 10, 30 )
physics.addBody( projectile, 'dynamic' )
projectile.gravityScale = 0
projectile.isBullet = true
projectile:setLinearVelocity( 0, -600 )
end
end
Runtime:addEventListener( 'touch', shoot )
This was suggested on the Corona forums by the Corona staff.
To remove each bullet that hits the sensor you need to give the projectile a 'type' of something along those lines. Note that you can use any word instead of 'type', but this is my preferred way.
projectile.type = 'bullet'
Then, you need to add an event listener to the sensor object that detects the collision, in this case it is an object called 'wall'. On collision you want to remove the other object that was in the collision ( the bullet ). You can do this like so.
local function wallCollision( event )
if event.phase == 'began' then
if event.other.type == 'bullet' then
display.remove( event.other )
event.other = nil
end
end
end
wall:addEventListener( 'collision', wallCollision )
'event.other' targets the other object involved in the collision event, in this case, the 'bullet'.
Not sure this is what you are after, but the strategy to handle evolving multiple objects which can be removed later as a result of collision is:
create the bullet display object, with a collision handler
in the collision handler, if object needs to be removed then use removeSelf; other changes may require delayed change as explained in Modifying Objects.
So in your shoot function you would add, after projectile:setLinearVelocity:
projectile.collision = function (event)
...
if remove then
self:removeSelf()
end
...
end
projectile:addEventListener( "collision", projectile)
This adds the handler to each bullet. You could instead add just one handler for the sensor, it would be similar code which you would put right after creating the sensor, except that you remove the event.other instead of self:
sensor.collision = function (event)
...
if remove then
event.other:removeSelf()
end
...
end
sensor:addEventListener( "collision", sensor)

Issues with Corona Storyboard Scenes

I am trying to make a card game.. I am creating a scene, making the background,and adding an image where when the player touches he/she will be transfered to the next scene.
singlePlayer scene:
local storyboard = require("storyboard")
local singlePlayer = storyboard.newScene()
local card1,card2,card3
function singlePlayer:createScene(event )
local group = self.view
-- body
local bg = display.newImage("bg.png")
bg.x = 100 ; bg.y = 50
group:insert(bg)
end
function singlePlayer:enterScene( event )
local group = self.view
local count = math.random(3)
local storyboard = require("storyboard")
local singlePlayer = storyboard.newScene()
local card1,card2,card3
function singlePlayer:createScene(event )
local group = self.view
-- body
local bg = display.newImage("bg.png")
bg.x = 100 ; bg.y = 50
group:insert(bg)
end
function singlePlayer:enterScene( event )
local group = self.view
local count = math.random(3)
if(count == 1) then
card1 = display.newImage("attack.png")
card1.x = 50 ; card1.y = 150
group:insert(card1)
else
card1 = display.newImage("ability.png")
card1.x = 50 ; card1.y = 150
group:insert(card1)
end
function card1:touch(event )
print("ok")
if(event.phase == "ended") then
storyboard.gotoScene("opponent_scene")
else
end
-- body
end
card1:addEventListener("touch",card1)
-- body
end
function singlePlayer:exitScene(event)
local group = self.view
card1:removeEventListener("touch",card1)
end
singlePlayer:addEventListener("createScene",singlePlayer)
singlePlayer:addEventListener("enterScene",singlePlayer)
singlePlayer:addEventListener("exitScene",singlePlayer)
return singlePlayer
Opponent scene:
local storyboard = require("storyboard")
local opponent_scene = storyboard.newScene()
function opponent_scene:createScene(event )
print("opponent_scene created")
-- body
end
function opponent_scene:enterScene(event )
print("opponent_scene enter")
local group = self.view
storyboard.removeScene("judge")
local text = display.newText("Opponent's turn",150,200)
storyboard.gotoScene("judge")
-- body
end
function opponent_scene:exitScene(event )
-- body
local group = self.view
end
opponent_scene:addEventListener("createScene",opponent_scene)
opponent_scene:addEventListener("enterScene",opponent_scene)
opponent_scene:addEventListener("exitScene",opponent_scene)
return opponent_scene
Judge scene:
local storyboard = require("storyboard")
local judge = storyboard.newScene()
function judge:createScene(event )
local group = self.view
local bg = display.newImage("destiny.png")
storyboard.removeScene("opponent_scene")
storyboard.gotoScene("singlePlayer")
-- body
end
judge:addEventListener("createScene",judge)
return judge
Will anyone explain to me what is going on with these scenes?
All i want is to make the game wait for the player's input (touching of the card)
After two clicks on the icon, storyboard is taken to opponent scene and it just shows on the screen the text "opponent's turn". What I want to do is for the text to appear briefly and then the scene to be taken to the player scene
Move your scene changing code inside a delayed function with timer.delay.. Here is what I did for my game to change screen after a brief time..
local function onSceneTouch( self, event )
if event.phase == "began" then
-- write all your other code here
-- function to change screen
function myClosure()
storyboard.gotoScene("opponent_scene")
end
-- Delay the call of closure function by 2 second (2000 milliseconds)
timer.performWithDelay( 2000, myClosure, 1 )
end
end
then call onSceneTouch function on touch of the screen
The single player enterscene code is wrong ,because you cant have two global function with the same name, the approach is not the right one . why do you call create scene inside the enter frame. suppose what you have done is correct then you should add event listeners to the function which you create in the enterscene as did earlier
singlePlayer:addEventListener("createScene",singlePlayer)
singlePlayer:addEventListener("enterScene",singlePlayer)
singlePlayer:addEventListener("exitScene",singlePlayer)
First of all please read the documentation of the storyboard and look the sample.
http://docs.coronalabs.com/api/library/storyboard/
But i request you to use composer instead of storyboard : http://docs.coronalabs.com/api/library/composer/index.html

Getting character to jump

I've created a sprite sheet(of a frog jumping) using texture packer and I am trying to get the character to jump forward when I click on the sprite. I've created an event listener and when I click on the sprite the play() method animates the sprite. But I can't get the sprite to jump forward using the applyForce or setLinearVelocity methods? Here is my code:
require("physics")
local sprite = require "sprite"
local sheetData = require "myFrogs" -- name of file created using texturepacker
physics.start()
physics.setGravity(0,1)
local _w = display.contentWidth/2
local _h = display.contentHeight/2
local spriteData = sheetData.getSpriteSheetData()
local spriteSheet = sprite.newSpriteSheetFromData("images/myFrogs.png", spriteData)
local spriteSet = sprite.newSpriteSet(spriteSheet, 1, 7) number of images in spritesheet
local frogSprite = sprite.newSprite(spriteSet)
frogSprite.x = _w
frogSprite.y = _h
physics.addBody(frogSprite, "static", {friction=1.0,density=1.0,bounce=0.3, radius=35})
frogSprite.isFixedRotation = true
local function frogJump(event)
if(event.phase == "ended") then
--frogSprite:applyForce() -- should I use this method
--frogSprite:setLinearVelocity() -- or this method
frogSprite:play()
end
end
frogSprite:addEventListener("touch", frogJump)
You made the frog's body static - switch to dynamic and it will jump using either of those methods.
By making frog body static, it will not be affected by impulse or force so it should be Dynamics
Body:applyForce( xForce, yForce, bodyX, bodyY ) <- Apply Force to your center of mass i.e Reference point you have set.
Body:setLinearVelocity( xVelocity, yVelocity ) <- It will give a velocity of data to will provide in terms of pixel per second.

In Corona SDK the background image always cover other images

I'm currently making a tower defense game with Corona SDK. However, while I'm making the gaming scene, The background scene always cover the monster spawn, I've tried background:toBack() ,however it's doesn't work.Here is my code:
module(..., package.seeall)
function new()
local localGroup = display.newGroup();
local level=require(data.levelSelected);
local currentDes = 1;
monsters_list = display.newGroup()
--The background
local bg = display.newImage ("image/levels/1/bg.png");
bg.x = _W/2;bg.y = _H/2;
bg:toBack();
--generate the monsters
function spawn_monster(kind)
local monster=require("monsters."..kind);
newMonster=monster.new()
--read the spawn(starting point) in level, and spawn the monster there
newMonster.x=level.route[1][1];newMonster.y=level.route[1][2];
monsters_list:insert(newMonster);
localGroup:insert(monsters_list);
return monsters_list;
end
function move(monster,x,y)
-- Using pythagoras to calauate the moving distace, Hence calauate the time consumed according to speed
transition.to(monster,{time=math.sqrt(math.abs(monster.x-x)^2+math.abs(monster.y-y)^2)/(monster.speed/30),x=x, y=y, onComplete=newDes})
end
function newDes()
currentDes=currentDes+1;
end
--moake monster move according to the route
function move_monster()
for i=1,monsters_list.numChildren do
move(monsters_list[i],200,200);
print (currentDes);
end
end
function agent()
spawn_monster("basic");
end
--Excute function above.
timer2 = timer.performWithDelay(1000,agent,10);
timer.performWithDelay(100,move_monster,-1);
timer.performWithDelay(10,update,-1);
move_monster();
return localGroup;
end
and the monster just stuck at the spawn point and stay there.
but, When i comment these 3 lines of code:
--local bg = display.newImage ("image/levels/1/bg.png");
--bg.x = _W/2;bg.y = _H/2;
--bg:toBack();
The problem disappear
Any ideas??Thanks for helping
Since you are using director, you should insert all your display objects into the localGroup.
You haven't inserted bg into localGroup.
SO director class inserts bg finally after inserting localGroup.
Modify your code as
--The background
local bg = display.newImage (localGroup,"image/levels/1/bg.png");
bg.x = _W/2;bg.y = _H/2;
bg:toBack();
or add the code
localGroup:insert(bg)
In more recent versions of Corona SDK:
Composer is the official scene (screen) creation and management library in Corona SDK.... The primary object in the Composer library is the scene object...and it contains a unique self.view.... This self.view is where you should insert visual elements pertaining to the scene.
So now in your scene:create() method, you should insert all DisplayObjects into self.view. It looks like this:
local composer = require( "composer" )
local scene = composer.newScene()
function scene:create()
local sceneGroup = self.view
local bg = display.newImage( ...
bg.x, bg.y = ...
local dude = display.newImage( ...
dude.x, dude.y = ....
sceneGroup:insert(bg)
sceneGroup:insert(dude)
end
The layering of DisplayObjects in this sceneGroup depends on the order in which they are added to the group, with the object added last on top. You can control this ordering with the toBack() and toFront() methods.

Resources