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.
Related
I wanted to make a Gui that when you press a TextButton to size your BodyWidth, BodyDepth, Head and BodyHeight and also change your walkspeed nad jumpower. I wanted it to understand when you clicked the button and run the script but I can't do it alone.
Here is the script:
--The error is here
game.Players.PlayerAdded:Connect(function(Player)
--Maybe here too
Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character.Humanoid
Humanoid.BodyDepthScale.Value = 10
Humanoid.BodyHeightScale.Value = 10
Humanoid.BodyWidthScale.Value = 10
Humanoid.HeadScale.Value = 10.5
Humanoid.WalkSpeed = 70
Humanoid.JumpPower = 0
end)
end)
end)
I tried to make an if condition But I still couldn't do the script.
First add a LocalScript to the button, and type:
local LocalPlayer = game:GetService("Players").LocalPlayer
local char = LocalPlayer.Character
local Humanoid = char:WaitForChild("Humanoid")
function Click ()
Humanoid.WalkSpeed = 70
Humanoid.JumpPower = 0
-- scale the meshes right here like char.Head.Size = Vector3.new(3,3,3)
end
script.Parent.MouseButton1Up:Connect(Click)
You can scale the meshes not the Humanoid Values
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
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
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.
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.