Lua remove crates - lua

I'm trying to make a simple game but i have ome problems.
I have a game and there is a layer of 6 boxes, 5 sec later there is another layer of 6 boxes and so on..
My problem is that i want to remove 2 random boxes from each layer. in the first layer there will be some kind of object that can jump on the boxes so i want to remove 2 boxes in the first layer and put an object there lik an animal. this is the code that i have (it's only for the first layer of boxes but when i know how to do the first layer it will also work on the second layer)
local physics = require("physics")
physics.start()
display.setDrawMode("hybrid")
local sky = display.newImage("bkg_clouds.png")
sky.x = display.contentWidth/2
sky.y = display.contentHeight/2-60
local ground = display.newImage("ground.png")
ground.x = display.contentWidth/2
ground.y = sky.y + display.contentHeight/2 + 80
ground.rotation = 0.7
local image_outline = graphics.newOutline( 2, "ground.png" )
physics.addBody( ground , "static", {friction = 0.5, bounce = 0.2, outline = image_outline})
local leftwall = display.newRect(0,0,1,display.contentHeight*2+100)
local rightwall = display.newRect(display.contentWidth,0,1,display.contentHeight*2+100)
physics.addBody(leftwall, "static",{bounce=0.1})
physics.addBody(rightwall, "static", {bounce = 0.1})
for i = 27, display.contentWidth-20, display.contentWidth/6 do
local crate = display.newImage("crate.png")
crate.x = i
crate.width = display.contentWidth/6.5
crate.y = 50
crate.height = display.contentWidth/6.5
physics.addBody(crate, {density = 3.0, friction = 0.5, bounce = 0.1})
end

This is probably a really basic solution, but the first thing that comes to mind is generating 2 random indeces you want to use to replace the crates.
So when your loop counter matches the index, then you draw an animal instad of a create.
I was thinking about something like this:
local physics = require("physics")
local math = require("math") -- not sure if you need to require it.
physics.start()
display.setDrawMode("hybrid")
local sky = display.newImage("bkg_clouds.png")
sky.x = display.contentWidth/2
sky.y = display.contentHeight/2-60
local ground = display.newImage("ground.png")
ground.x = display.contentWidth/2
ground.y = sky.y + display.contentHeight/2 + 80
ground.rotation = 0.7
local image_outline = graphics.newOutline( 2, "ground.png" )
physics.addBody( ground , "static", {friction = 0.5, bounce = 0.2, outline = image_outline})
local leftwall = display.newRect(0,0,1,display.contentHeight*2+100)
local rightwall = display.newRect(display.contentWidth,0,1,display.contentHeight*2+100)
physics.addBody(leftwall, "static",{bounce=0.1})
physics.addBody(rightwall, "static", {bounce = 0.1})
-- Generates 2 random indeces, falling in the loop boundary
local index_1 = math.random(27, display.contentWidth-20)
local index_2 = math.random(27, display.contentWidth-20)
for i = 27, display.contentWidth-20, display.contentWidth/6 do
-- Draw animal for matching index, otherwise a crate.
if (i == index_1 or i == index_w) then
-- draw animal
else
local crate = display.newImage("crate.png")
crate.x = i
crate.width = display.contentWidth/6.5
crate.y = 50
crate.height = display.contentWidth/6.5
physics.addBody(crate, {density = 3.0, friction = 0.5, bounce = 0.1})
end
end
As I said, really basic implementation, but should get the trick working for your layer.
Open issues for the implementation:
You might have a layer without animals, as your index might not fall in your generated steps.
Your indeces might overlap, resulting in one animal
Your index might be at the start or end only, altering/beaking your game.
But I'll leave it up to you to deal with those checks.

Related

Roblox, how to make something only tween in one direction?

I tried doing
local fakeRightArm = Instance.new("Part")
fakeRightArm.BottomSurface = Enum.SurfaceType.Smooth
fakeRightArm.CanCollide = false
fakeRightArm.Massless = true
fakeRightArm.Material = Enum.Material.Plastic
fakeRightArm.BrickColor = RightArm.BrickColor
fakeRightArm.Size = RightArm.Size
fakeRightArm.CFrame = RightArm.CFrame
fakeRightArm.Parent = character
fakeRightArm.Name = "fakeRightArm"
local weld = Instance.new("WeldConstraint")
weld.Part0 = RightArm
weld.Part1 = fakeRightArm
weld.Parent = character
weld.Name = "FakeArmWeld"
tweenService:Create(fakeRightArm, TweenInfo.new(0.3, Enum.EasingStyle.Quad), {Size = fakeRightArm.Size + Vector3.new(0, 50, 0), CFrame = fakeRightArm.CFrame * CFrame.new(0,0,-50)}):Play()
but that seems to not work. It's probably because of the weld.
I want it so it tweens the arm forward.
DevForum post (maybe more detailed): https://devforum.roblox.com/t/how-to-only-tween-in-one-direction/841408/42
Thank you!
don't think you need the tween function just to position it
https://developer.roblox.com/en-us/api-reference/datatype/CFrame
start with the right arm position, then rotate its coordinate frame.
local fakeRightArm = Instance.new("Part")
fakeRightArm.BottomSurface = Enum.SurfaceType.Smooth
fakeRightArm.CanCollide = false
fakeRightArm.Massless = true
fakeRightArm.Material = Enum.Material.Plastic
fakeRightArm.BrickColor = RightArm.BrickColor
fakeRightArm.Size = RightArm.Size
fakeRightArm.Parent = character
fakeRightArm.Anchored = true
fakeRightArm.Name = "fakeRightArm"
-- you pick which angle to make it face
local Xdir = math.rad( 0 ) -- twist palm up, palm down
local Ydir = math.rad( 90 ) -- lift up / down
local Zdir = math.rad( 15 ) -- swing left / right
fakeRightArm.CFrame = RightArm.Position *CFrame.Angles( Xdir, Ydir, Zdir )

How to make a smooth projectile in roblox?

I am trying to make a projectile (Energy ball) for my game. I am using body velocities, but the issue is is that it seems laggy. I shoot it then half a second through flight it seems to stop in mid air for a quarter of a second then continue. This only happens the first minute of playing.
I tested shooting at the ground so at an angle so it will go slower, but it seems the the ball freezes when it reaches a certain distance from the player, not a certain time.
The actual projectile is a less transparent ball inside of a bigger, more transparent ball, connected by a weld. If I remove one of the balls so the projectile is just one ball, my issue is gone. How can I remove the issue while keeping both balls?
Here is my code that I tried:
EnergyBallEvent.OnServerEvent:Connect(function(Player, MouseClickLocation)
local Character = Player.Character
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
if Humanoid.Health > 0 and not DisabledItems[Player.UserId..'EnergyBall'] then
DisabledItems[Player.UserId..'EnergyBall'] = true
Humanoid:LoadAnimation(script.EnergyBallAnimation):Play()
local Ball = Instance.new("Part", SpellsFolder)
local CasterValue = Instance.new("ObjectValue", Ball)
CasterValue.Name = "Caster"
CasterValue.Value = Player.Character
Ball:SetNetworkOwner(nil) --removing or modifying this
Ball.Transparency = 1
Ball.Shape = Enum.PartType.Ball
Ball.BrickColor = BrickColor.new(0, 255, 255)
Ball.Material = Enum.Material.Neon
Ball.Size = Vector3.new(2.2, 2.2, 2.2)
Ball.Massless = true
Ball.CanCollide = false
local Weld = Instance.new("Weld", Ball)
Weld.Part0 = Ball
Ball = Instance.new("Part", Ball)
Ball:SetNetworkOwner(nil) --or this have no effect that is visible
Ball.Transparency = 1
Ball.Shape = Enum.PartType.Ball
Ball.BrickColor = BrickColor.new(0, 255, 255)
Ball.Material = Enum.Material.Neon
Ball.Size = Vector3.new(1.6, 1.6, 1.6)
Ball.Massless = true
Weld.Part1 = Ball
Weld = Instance.new("Weld", Ball)
Weld.Part0 = Ball
if Character:FindFirstChild("LeftHand") then
Weld.Part1 = Character.LeftHand
else
end
for i = 0, 40, 1 do
Ball.Transparency = Ball.Transparency - 0.012
Ball.Parent.Transparency = Ball.Parent.Transparency - 0.006
wait(0.02)
end
Weld:Destroy()
local Motion = Instance.new("BodyVelocity", Ball)
Motion.Velocity = ((MouseClickLocation - Player.Character.HumanoidRootPart.Position).Unit*100)
DisabledItems[Player.UserId..'EnergyBall'] = nil
end
end)
Here is some more code I tried:
EnergyBallEvent.OnServerEvent:Connect(function(Player, MouseClickLocation)
local Character = Player.Character
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
if Humanoid.Health > 0 and not DisabledItems[Player.UserId..'EnergyBall'] then
DisabledItems[Player.UserId..'EnergyBall'] = true
Humanoid:LoadAnimation(script.EnergyBallAnimation):Play()
local Model = Instance.new("Model", SpellsFolder)
local Ball = Instance.new("Part", Model)
local CasterValue = Instance.new("ObjectValue", Model)
CasterValue.Name = "Caster"
CasterValue.Value = Player.Character
Ball.Transparency = 1
Ball.Shape = Enum.PartType.Ball
Ball.BrickColor = BrickColor.new(0, 255, 255)
Ball.Material = Enum.Material.Neon
Ball.Size = Vector3.new(2.2, 2.2, 2.2)
Ball.Massless = true
Ball.CanCollide = false
local Weld = Instance.new("Weld", Ball)
Weld.Part0 = Ball
local Ball2 = Instance.new("Part", Model)
Model.PrimaryPart = Ball2
Ball2:SetNetworkOwner(nil)
Ball2.Transparency = 1
Ball2.Shape = Enum.PartType.Ball
Ball2.BrickColor = BrickColor.new(0, 255, 255)
Ball2.Material = Enum.Material.Neon
Ball2.Size = Vector3.new(1.6, 1.6, 1.6)
Ball2.Massless = true
Weld.Part1 = Ball2
Weld = Instance.new("Weld", Ball2)
Weld.Part0 = Ball2
if Character:FindFirstChild("LeftHand") then
Weld.Part1 = Character.LeftHand
else
end
for i = 0, 40, 1 do
Ball2.Transparency = Ball2.Transparency - 0.012
Ball.Transparency = Ball.Transparency - 0.006
wait(0.02)
end
Weld:Destroy()
local Motion = Instance.new("BodyVelocity", Ball)
Motion.Velocity = ((MouseClickLocation - Player.Character.HumanoidRootPart.Position).Unit*100)
DisabledItems[Player.UserId..'EnergyBall'] = nil
end
end)
I hate this behaviour and I NEED to fix it
Another observation is if I comment out the second ball creation and stuff so it is a just one ball, the bug is gone
You want to add the propulsion code to the clientside script in an OnClientEvent:Connect() block, and make the serverscript instead have Ball:SetNetworkOwner(Player) and EnergyBallEvent:FireClient(Ball). I'm OP but I might as well answer this question now since I figured it out long ago.

How to create a heap of balls and keep it within the screen on ground in lua and corona sdk?

How to create a heap of balls and keep it within the screen on the ground in lua and corona sdk?
Right now it is falling down from the screen and disappear from the screen.
local physics = require("physics")
physics.start()
--local sky = display.newImage("sky.png")
--sky:scale( 5, 10 )
--sky.x = -100
--sky.y = -200
local field = display.newImage("field.png")
field:scale( 5, 10 )
field.x = 240
field.y = 470
local sky = display.newImage("sky.png")
sky:scale( 10, 3 )
physics.addBody(field,{friction = 0.5})
field.bodyType = "static"
local football = display.newImage("football.png")
football.x = 180
football.y = 80
football.rotation = 20
physics.addBody(football,{density = 2.0,friction = 0.5,bounce =0.5})
local function fallingball_field()
local football = display.newImage("football.png")
football.x = math.random(400)
football.y = -100
football.rotation = 20
physics.addBody(football, { density = 4.0,friction = 0.5, bounce = 0.5})
end
timer.performWithDelay(200, fallingball_field,200)
In order to put your physics objects within the screen you must create a "Boundary box" that will keep your ball within the screen. Bellow is an example on how to create a boundary:
local physics = require("physics")
physics.start()
physics.setDrawMode("hybrid")
local myCircle = display.newCircle( 100, 100, 30 )
myCircle:setFillColor( 0.5 )
local leftWall = display.newRect(0,display.contentCenterY, 10, display.contentHeight)
local rightWall = display.newRect(display.contentWidth,display.contentCenterY, 10, display.contentHeight)
local bottomWall = display.newRect(display.contentCenterX, display.contentHeight - 25, display.contentWidth, 10)
local topWall = display.newRect(display.contentCenterX, 25, display.contentWidth, 10)
leftWall:setFillColor(nil)
rightWall:setFillColor(nil)
bottomWall:setFillColor(nil)
topWall:setFillColor(nil)
physics.addBody (leftWall, "static", { bounce = 0.1} )
physics.addBody (rightWall, "static", { bounce = 0.1} )
physics.addBody (bottomWall, "static", { bounce = 0.1} )
physics.addBody (topWall, "static", { bounce = 0.1} )
physics.addBody (myCircle, "dynamic", { bounce = 1.5} )
Explanation:
The "walls" will keep your physics objects into place assuming that your app is set to horizontal this will work as is. let me know if your application is vertical.
The physics.setDrawMode("hybrid") will show the physics boundaries of EACH objects that you set with a physics body.It will be easier to see how your objects interact to each other.

Transition.to not moving object properly - Corona SDK

I have a table full of enemies and simple want them to move across the screen via gameLoop, however for some reason nothing seems to work. Its probably an easy fix but I have tried to fix it but am getting nowhere. Anyone know whats up?
gameLoop Function
local i
for i = 1, #enemies do--.numChildren,1, -1 do
local blocks = enemies[i]
if blocks ~= nil and blocks.x ~= nil then
enemyRate = 2.0 + (0.1 * wave)
transition.to( blocks, { time=1500, x=300} )
end
end
The Spawn Function
function spawnEnemy()
local spawnData = { -- Easily store spawns in a table to make it easier to add new enemies later
{name = "Blue", seq = "blueRect", frame = 3, imgSheet = imageSheetRectangle, seqData = sequenceDataRectangle},
{name = "Red", seq = "blueCross", frame = 1, imgSheet = imageSheetCross, seqData = sequenceDataCross},
{name = "Green", seq = "blueCirc", frame = 2, imgSheet = imageSheetCircle, seqData = sequenceDataCircle}
}
local xPos = display.contentWidth - 150
local r = math.random(1, #spawnData)
local sd = spawnData[r] -- get the spawn data for this enemy
local s = display.newSprite(sd.imgSheet, sd.seqData)
s.name = sd.name
physics.addBody(s, { isSensor = true })
s:setSequence(sd.seq)
s:setFrame(sd.frame)
s.y = display.contentHeight - 400
s.x = xPos
enemies[#enemies+1] = s
enemyGroup:insert(s)
In this related answer: transition.to( ) doesn't work within a function and with a Runtime:addEventListener( "enterFrame", method) listener in Corona / Lua you can see a similar issue as I stated above. You are creating an animation right as one is starting - making it seem as though it is not moving. As I suggested above, if it suits your game, begin the transition when you spawn the object; not every gameloop.

How to Spawn multiple objects every 10 seconds

How to make this spawn 'math.random(1,3)' smile.png every 10 seconds , and delete the smile.png after the left screen
<code>
local physics = require ("physics");
physics.start();
local function listener(me)
transition.to (me, {time = math.random(1000,4000), x = math.random(10,310), y = -30, onComplete = function()listener(me)end});
end
--Spawning multiple objects in randoms locations
local function spawnsmile()
local smile = display.newImageRect("smile.png", 45, 45);
smile:setReferencePoint(display.CenterReferencePoint);
smile.x = math.random(-10, 400);
smile.y = -40;
transition.to( smile, {time = math.random(2000, 8000), x = math.random(-10, 400) , y = 600,});
physics.addBody(smile, "dynamic", {density = 0.1, bounce = 0.1, friction = .1, radius = 0});
--Adding touch event
smile:addEventListener("touch", smile);
end
tmr = timer.performWithDelay(0, spawnsmile, total_smiles);
<code>
Regards Kevin
Your code was missing total_smiles value assignment and delay argument.
Working code:
local physics = require ("physics");
physics.start();
local function listener(me)
transition.to (me, {time = math.random(1000,4000), x = math.random(10,310), y = -30, onComplete = function()listener(me)end});
end
--Spawning multiple objects in randoms locations
local function spawnsmile()
local smile = display.newImageRect("Button.png", 45, 45);
smile:setReferencePoint(display.CenterReferencePoint);
smile.x = math.random(-10, 400);
smile.y = -40;
transition.to( smile, {time = math.random(2000, 8000), x = math.random(-10, 400) , y = 600,});
physics.addBody(smile, "dynamic", {density = 0.1, bounce = 0.1, friction = .1, radius = 0});
--Adding touch event
smile:addEventListener("touch", smile);
end
local total_smiles = 15
tmr = timer.performWithDelay(10000, spawnsmile, total_smiles);
Moreover, you should store references to created smiles in order to properly destroy them and don't leak memory. more info on memory managment
local smiles = {}
table.insert(smiles, smile)
And disposal:
for i=#smiles,1,-1 do
smiles[i]:removeSelf()
smiles[i] = nil
end
change your timer to perform every 10.000 ms instead of 0. And your listener function doesnt really fill any purpose, remove that and change your transition.to inside of spawnsmile function to
transition.to( smile, {time = math.random(2000, 8000), x = math.random(-10, 400) , y = 600, onComplete = function(obj) obj:removeSelf() obj = nil end});
That should do what you want it to do =) Also there needs to be a value inside total_smiles, but i guess you have it elsewhere.

Resources