Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

For Loop Problem

Sometimes when I try to spawn entities through a for loop, I want it to spawn a set amount of entities into the scene, but most of the time, it doesn’t spawn as many as I want it to. For example, if I have a number that increases by 1 every time you touch the screen, that is how many entities I want to spawn. But sometimes, for some weird reason it doesn’t spawn all of them. Can somebody please shed some light on this situation???

Here’s all the code in my project (fair warning, it consists of a bit over 1000 lines of code, so yeah :/)

-- Bean

function setup()
    -- Create a new craft scene
    scene = craft.scene()
    scene.sky.material.sky = color(0, 240, 255)
    scene.sky.material.horizon = color(0, 131, 255)
    scene.sky.material.ground = color(0, 131, 255)
    parameter.watch("bean.rotation.x")
    parameter.watch("weaponry[b].position")
    parameter.integer("CloudsAlpha", 0, 100, 0)
    viewer.mode = FULLSCREEN
    bobActive = false
    pm = false
    hit = false
    swingshot = false
    spawned = false
    dead = false
    equipped = false
    enemyDead = false
    dead1 = false
    dead2 = false
    notDead = true
    weaponDeployed = false
    shoot = true
    gtp = false
    reloading = false
    spawn = true
    crateSpawn = false
    recoil = false
    setRecoil = false
    reload = false
    spawnCrate = true
    explode = false
    musicPlaying = false
    shooting = false
    following = false
    followingButton = false
    shielding = false
    shieldingButton = false
    shieldingAction = false
    lock = false
    chasing = false
    stay = true
    sad = false
    deathSound = false
    deathAnim = false
    restartAnim = false
    done = false
    done2 = false
    restartButton = false
    sniperTargetPlaced = false
    startHeartCollision = false
    sniperTarget = {x = 0, y = 0}
    health = 500
    bulletSpeed = 2
    enemySpeed = 1
    weaponSelected = 1
    buttonWidth = {s = 0}
    cr = 0
    tr = 0
    tp = vec2(0, 0)
    br = 0
    br2 = 0
    bc = 0
    hc = 0
    nbr = 4
    et = 0
    et2 = 0
    et3 = 0
    sp = 1
    wave = 1
    weaponRot = 0
    camRot = 0
    ammo = 25
    cnt = 0
    shr = 0
    shrc = 1
    hr = 0
    jS = {x = 150, y = 126}
    level = readLocalData("level", 0)
    bobsKilled = readLocalData("total kills", 0)
    bobsKilled2 = 0
    choice = math.random(1,3)
    explosion = math.random(1, 3)
    bobs = {}
    followers = {}
    touches = {}
    moveDir = vec3(0, 0, 0)
    weapons = {sword = craft.model(asset.documents.Dropbox.blasterN_obj);
               blaster = craft.model(asset.documents.Dropbox.blasterA_obj);
               pistol = craft.model(asset.documents.Dropbox.blasterG_obj);
               sniper = craft.model(asset.documents.Dropbox.blasterE_obj)}
    weaponry = {}
    raycasts = {}
    bullets = {}
    crates = {}
    hearts = {}
    bobDefenses = {}
    spikes = {}
    cS = {x = .1, y = .1}
    forward = scene.camera.forward * moveDir.z
    right = scene.camera.right * moveDir.x
    up = vec3(0,1,0) * moveDir.y

    finalDir = forward + right + up

    -- Create a new entity
    bean = scene:entity()
    b = bean:add(craft.rigidbody, DYNAMIC)
    b.sleepingAllowed = false
    b.angularDamping = 0.4
    bean.model = craft.model(asset.builtin.Primitives.Capsule)
    bean:add(craft.shape.model, bean.model)
    bean.position = vec3(0, -1, 0)
    bean.scale = vec3(3, 3, 3) / 8
    bean.material = craft.material(asset.builtin.Materials.Standard)

    hat = scene:entity()
    hat.model = craft.model(asset.documents.Dropbox.Army_Hat_obj)
    hat.position = vec3(0, -0.575, 0)
    hat.scale = vec3(0.15, 0.15, 0.15) / 8

    lob = scene:entity()
    lb = lob:add(craft.rigidbody, STATIC)
    lb.sleepingAllowed = false
    lob.model = craft.model(asset.builtin.Primitives.Sphere)
    lob.position = vec3(bean.x + 1, -0.75, bean.z)
    lob.scale = vec3(1, 1, 1) / 8
    lob.material = craft.material(asset.builtin.Materials.Standard)

    createGround(vec3(0, -3.12, 0))
    shieldGen()
    -- Create boundaries for game arena
    makeWalls(vec3(4.5, -1, 0), vec3(4, 8, 32) / 8)
    makeWalls(vec3(-4.5, -1, 0), vec3(4, 8, 32) / 8)
    makeWalls(vec3(0, -1, 4.5), vec3(32, 8, 4) / 8)
    makeWalls(vec3(0, -1, -4.5), vec3(32, 8, 4) / 8)
    scene.camera.position = vec3(-5, 2.5, -5)
    scene.camera.eulerAngles = vec3(25, 45, 0)
    cameraSettings = scene.camera:get(craft.camera)
    cameraSettings.ortho = true
    cameraSettings.orthoSize = 4
end

function createGround(p)
    ground = scene:entity()
    g = ground:add(craft.rigidbody, STATIC)
    g.restitution = 1
    ground.name = "Ground"
    ground.model = craft.model.cube(vec3(8, 4.5, 8))
    ground:add(craft.shape.box, vec3(8, 4.5, 8))
    ground.position = p
    ground.material = craft.material(asset.builtin.Materials.Standard)
    ground.material.map = readImage(asset.builtin.Blocks.Sand)
end

function createSubject(p)
    bob[bc] = scene:entity()
    bob[bc].name = "Bob No."..bc
    bob[bc].model = craft.model(asset.builtin.Primitives.Sphere)
    bob[bc].position = p
    bob[bc].scale = vec3(1, 1, 1) / 8
    bob[bc].material = craft.material(asset.builtin.Materials.Standard)
    bc = bc + 1
end

function spikeGen(p)
    spike = scene:entity()
    spike.model = craft.model(asset.documents.Dropbox.blasterE_obj)
    spike.position = p
    spike.scale = vec3(.7, .7, .7) / 8
    spike.material = craft.material(asset.documents.Dropbox.Laser)
    return({spike = spike})
end

function ps(s, id)
    sound(s, id)
end

function weaponGen()
    for i=1, 1 do
        if weaponSelected == 1 then
            weapon = scene:entity()
            w = weapon:add(craft.rigidbody, STATIC)
            weapon.model = weapons.sword
            w.sleepingAllowed = false
            w.interpolate = true
            weapon.eulerAngles = vec3(0, 180, 0)
            weapon.scale = vec3(10, 10, 10) / 8
            table.insert(weaponry, weapon)
        elseif weaponSelected == 2 then
            weapon2 = scene:entity()
            w2 = weapon2:add(craft.rigidbody, STATIC)
            weapon2.model = weapons.blaster
            w2.sleepingAllowed = false
            w2.interpolate = true
            weapon2.eulerAngles = vec3(0, 180, 0)
            weapon2.scale = vec3(10, 10, 10) / 8
            table.insert(weaponry, weapon2)
        elseif weaponSelected == 3 then
            weapon3 = scene:entity()
            w3 = weapon3:add(craft.rigidbody, STATIC)
            weapon3.model = weapons.pistol
            w3.sleepingAllowed = false
            w3.interpolate = true
            weapon3.eulerAngles = vec3(0, 0, 0)
            weapon3.scale = vec3(10, 10, 10) / 8
            table.insert(weaponry, weapon3)
        elseif weaponSelected == 4 then
            weapon4 = scene:entity()
            w4 = weapon4:add(craft.rigidbody, STATIC)
            weapon4.model = weapons.sniper
            w4.sleepingAllowed = false
            w4.interpolate = true
            weapon4.eulerAngles = vec3(0, 0, 0)
            weapon4.scale = vec3(10, 10, 10) / 8
            table.insert(weaponry, weapon4)
        end
    end
end

-- Create shield for player
function shieldGen()
    shield = scene:entity()
    sh = shield:add(craft.rigidbody, STATIC)
    shield.model = craft.model(asset.builtin.CastleKit.shieldBlue_obj)
    shield:add(craft.shape.model, shield.model)
    shield.position = vec3(10, 10, 10)
    shield.eulerAngles = vec3(0, 0, 0)
    shield.scale = vec3(1.7, 1.7, 1.7) / 8
end

function makeWalls(p, s)
    wall = scene:entity()
    wa = wall:add(craft.rigidbody, STATIC)
    wa.sleepingAllowed = false
    wall:add(craft.shape.box, s + s + s + s)
    wall.position = p
    wall.scale = s
end

function coinGen(p)
    coin = scene:entity()
    coin.model = craft.model(asset.documents.Dropbox.coinGold_obj)
    coin.position = p
    coin.eulerAngles = vec3(0, 0, 0)
    coin.scale = vec3(1, 1, 1) / 8
end

function BulletGen(p)
    bullet = scene:entity()
    bub = bullet:add(craft.rigidbody, DYNAMIC)
    bub.sleepingAllowed = false
    bullet.model = craft.model(asset.builtin.Primitives.Sphere)
    bullet:add(craft.shape.sphere, 1)
    bullet.position = p
    bullet.scale = vec3(.07, .07, .07)
    bullet.rotation = quat.eulerAngles(90, 0, 0) 
    bullet.material = craft.material(asset.builtin.Materials.Specular)
    bullet.material.diffuse = color(255,165,0)
    return({bullet=bullet,bub=bub})
end

Comments

  • Posts: 150

    Here’s a bit more of the code:

    -- Create support crates
    function createCrates(p)
        crate = scene:entity()
        c = crate:add(craft.rigidbody, DYNAMIC)
        c.sleepingAllowed = false
        c.restitution = .5
        crate.model = craft.model(asset.documents.Dropbox.crateItem_obj)
        crate:add(craft.shape.model, crate.model)
        crate.position = p
        crate.rotation = quat.eulerAngles(0, 0, 0)
        crate.scale = vec3(7, 7, 7) / 8
        return({crate=crate, c=c})
    end
    
    -- Create health crates to regenerate players health
    function createHearts(p)
        heart = scene:entity()
        heart.model = craft.model(asset.documents.Dropbox.heart_obj)
        heart.position = p
        heart.scale = vec3(7, 7, 7) / 8
        return({heart=heart,h=h})
    end
    
    function createDeathGround(p)
        dground = scene:entity()
        dground.model = craft.model.cube(vec3(2, 2, 2))
        dground.position = p
        dground.material = craft.material(asset.builtin.Materials.Standard)
        dground.material.map = readImage(asset.builtin.Blocks.Sand)
    end
    
    function createDeathBean(p)
        dbean = scene:entity()
        dbean.model = craft.model(asset.builtin.Primitives.Capsule)
        dbean.position = p
        dbean.scale = vec3(4, 4, 4) / 8
        dbean.material = craft.material(asset.builtin.Materials.Standard)
    end
    
    function createClouds(p)
        clouds = scene:entity()
        clouds.model = craft.model(asset.builtin.Primitives.Plane)
        clouds.position = p
        clouds.scale = vec3(16, 4, 16) / 8
        clouds.material = craft.material(asset.documents.Dropbox.Clouds)
        clouds.material.CloudsPower1 = CloudsAlpha
    end
    
    function update(dt)
        -- Update the scene (physics, transforms etc)
        scene:update(dt)
        if equipped == true then
            if not shielding then
            for b=#weaponry,1,-1 do
                if recoil == false and reload == false and weaponSelected == 1 then
                    weaponry[b].position = bean.position + -weaponry[b].forward * 0.300 * 2.0
                end
                if recoil == false and reload == false and weaponSelected == 2 then
                    weaponry[b].position = bean.position + -weaponry[b].forward * 0.400 * 2.0
                end
                if recoil == false and reload == false and weaponSelected == 3 then
                    weaponry[b].position = bean.position + -weaponry[b].forward * 0.400 * 2.0
                end    
                if recoil == false and reload == false and weaponSelected == 4 then
                    weaponry[b].position = bean.position + -weaponry[b].forward * 0.570 * 2.0   
                end
                if recoil == true then
                    weaponry[b].position = bean.position + -weaponry[b].forward * 0.250 * 2.0
                    delay = tween.delay(0.25, function() recoil = false end)
                end
            end
        end
            if reload == false then
                b.type = DYNAMIC
            else
                b.type = STATIC
                b.linearVelocity = vec3(0, 0, 0)
            end
            if not shieldingAction then
                b.type = DYNAMIC
            else
                b.type = STATIC
            end
            hat.x = bean.x
            hat.y = bean.y - 0.08
            hat.z = bean.z
            bean.eulerAngles = vec3(0, CurrentTouch.x + CurrentTouch.y, 0)
            if shielding then
                if not shieldingAction then
                    shield.position = bean.position + -shield.forward * 0.208 * 2.0
                end
            else
                shrc = 1
            end
            for i=#bobs,1,-1 do
                bobs[i].sphere1.eulerAngles = -bean.forward
            end
        end
    
        for h=1,#hearts do
            hr = hr + 2
            hearts[h].heart.eulerAngles = vec3(0, hr, 0)
        end
    
        for i=1,#bobs do
            velocity = bobs[i].sphere1.position - bean.position
            bobs[i].ss.linearVelocity = -velocity*.5
        end 
    end
    
  • Posts: 150

    Here’s another chunk:

    -- Called automatically by codea 
    function draw()
        update(DeltaTime)
    
        -- Draw the scene
        scene:draw()    
        checkY()
        if equipped == false then
            checkWeaponCollision()
        else
            checkCollision()
            CheckCrateCollisionWithBean()
            checkShieldCollisionWithBobs()
            checkBobCollisionWithBobs()
            checkHeartCollisionWithBean()
        end
    
        explosion = math.random(1, 3)
    
        if dead == true then
            notDead = false
            if not deathSound then
                sound(asset.documents.Dropbox.Game_Over_Sound_Effect)
                sound(asset.documents.Dropbox.game_over)
            end
            deathSound = true
            if done == true then 
                if not restartButton then
                    sprite(asset.documents.Dropbox.shadedDark16,1000,585,80)
                else
                    sprite(asset.documents.Dropbox.flatDark15,1000,585,100)
                end
                font("Futura-CondensedExtraBold")
                fontSize(75)
                fill(81, 31, 119)
                text("YOU DIED!", WIDTH/2 + 344, HEIGHT/2 + 326)
                fill(225, 0, 255)
                text("YOU DIED!", WIDTH/2 + 340, HEIGHT/2 + 330)
                fontSize(40)
                fill(31, 78, 119)
                text("CARE TO TRY AGAIN?", WIDTH/2 + 344, HEIGHT/2 + 261)
                fill(11, 192, 252)
                text("CARE TO TRY AGAIN?", WIDTH/2 + 340, HEIGHT/2 + 265)
            end
                fill(0) 
                ellipse(WIDTH/2, HEIGHT/2, cS.x, cS.y)
                if deathAnim == false then
                    for i=#bobs,1,-1 do
                        bobs[i].sphere1:destroy()
                        table.remove(bobs, i)
                        for c=#crates,1,-1 do
                            crates[c].crate:destroy()
                            table.remove(crates, c)
                        end
                    end
                    cSOne = tween(2.5, cS, {x = 2000, y = 2000}, tween.easing.linear, function() done = true scene.sky.material.sky = color(255, 165, 0) scene.sky.material.horizon = color(255, 100, 0) scene.sky.material.ground = color(255, 0, 0) end)
                    cSTwo = tween(1, cS, {x = 0, y = 0}, tween.easing.linear)
                    tween.sequence(cSOne, cSTwo)
                end
                deathAnim = true
            end
    
    
           if done == true then
                createDeathGround(vec3(10, -1, 0))
                createDeathBean(vec3(10, 0.5, 0))
                created = true
                cameraSettings.ortho = false
                cameraSettings.orthoSize = 0
                scene.camera.eulerAngles = vec3(0, 0, 0)
                scene.camera.z = -4
                scene.camera.y = 1
                scene.camera.x = 8.5
            else
                scene.sky.material.sky = color(0, 240, 255)
                scene.sky.material.horizon = color(0, 131, 255)
                scene.sky.material.ground = color(0, 131, 255)
                notDead = true
                scene.camera.x = -5
                scene.camera.y = 2.5
                scene.camera.z = -5
                scene.camera.eulerAngles = vec3(25, 45, 0)
                cameraSettings.ortho = true
                cameraSettings.orthoSize = 4
            end
    
    
        if notDead == true then
            dead = false
        end
    
        -- Raycast shooting method for my game
        if shoot == true then
            for b=1,#weaponry do
                weaponRay = scene.physics:raycast(bean.position + -weaponry[b].forward / 0.06 * .5, bean.position + -weaponry[b].forward * 1 * 0.05, 100)
                uv = scene.debug:line(bean.position + -weaponry[b].forward / 0.06 * .5, bean.position +  -weaponry[b].forward / 1 * 0.05 * 1.7, color(255))
            end
    
            radius = scene.physics:spherecast(vec3(bean.x, bean.y, bean.z), vec3(.1, 1, 1), 1, 1)
    
            if weaponRay~=nil then
                rx=weaponRay.point.x
                ry=weaponRay.point.y
                rz=weaponRay.point.z  
                parameter.watch("weaponRay.point")      
                for a,b in pairs(bobs) do
                    if math.abs(bobs[a].sphere1.position.x-rx)<1 and math.abs(bobs[a].sphere1.position.y-ry)<1.75 and -math.abs(bobs[a].sphere1.position.y-ry)<1 and math.abs(bobs[a].sphere1.position.z-rz)<1.75 then
                        bobs[a].sphere1:destroy()
                        table.remove(bobs, a)
                        bc = bc - 1
                        bobsKilled = bobsKilled + 1
                        saveLocalData("total kills", bobsKilled)
                        bobsKilled2 = bobsKilled2 + 1
    
                        if sp ~= 30 then
                        if bc == 0 then
                            spawnCrate = true
                            sp = sp + 1
                            for i=1,0 + sp do
                                table.insert(bobs, createFirstSphere(vec3(math.random(-3,3),0,-1)))
                                bc = sp
                            end
                        end
                        if sp == 5 or sp == 10 or sp == 15 or sp == 20 or sp == 25 then
                            for i=1,1 do
                                if spawnCrate == true then
                                    table.insert(crates, createCrates(vec3(math.random(-3,3), 2, math.random(-2,2))))
                                    spawnCrate = false
                                end
                            end
                        end
                    end               
                end
            end
            end
        end
    
        countdown()
        if dead == false then
            font("Futura-CondensedExtraBold")
            fontSize(35)
            fill(81, 31, 119)
            text("ROUND: "..sp, WIDTH/2 + 4, HEIGHT/2 + 291)
            fill(225, 0, 255)
            text("ROUND: "..sp, WIDTH/2, HEIGHT/2 + 295)
            -- Ammo count
            fontSize(80)
            fill(44, 119, 31)
            text(ammo, WIDTH/2 - 336, HEIGHT/2 + 345)
            fill(73, 252, 11)
            text(ammo, WIDTH/2 - 340, HEIGHT/2 + 350)
            -- Health bar (placeholder)
            fontSize(35)
            noStroke()
            fill(0)
            rect(WIDTH/2 - 260, HEIGHT/2 + 315, 520, 70)
            fill(130, 35, 30)
            rect(WIDTH/2 - 250, HEIGHT/2 + 325, 500, 50)
            fill(0, 255, 0)
            rect(WIDTH/2 - 250, HEIGHT/2 + 325, health, 50)
            fill(0)
            text("SERGEANT BEAN", WIDTH/2, HEIGHT/2 + 350)
        end
        if ray ~= nil then
            text(ray.entity.z, 500, 500)
        end
    
        if bean.y <= -1.5 then
            if dead2 == false then
                tween(0.75, scene.camera, {position = vec3(-5, 2.5, -5)}, tween.easing.cubicOut)
            end
            dead2 = true
        end
    
        if following then
            if bean.y > -1.5 then
                scene.camera.x = bean.x - 5
                scene.camera.z = bean.z - 5
            end
        else
    
        end
    
        -- Draw controls
        -- Shooting 
        if dead == false then
        if not shooting then
            sprite(asset.documents.Dropbox.explosion2,980,130,150)
        else
            tint(210)    
            sprite(asset.documents.Dropbox.explosion2,980,130,180)   
        end
    
            noTint()
        if not shieldingButton then
            sprite(asset.documents.Dropbox.shadedDark18,1000,385,80)
        else
            sprite(asset.documents.Dropbox.flatDark17,1000,385,100)
        end
        end
    
        -- Inventory for weapons
        sprite(asset.documents.Dropbox.glassPanel_tab,990,720,100)
        if weaponSelected == 1 then
            sprite(asset.documents.Dropbox.blasterN_png,990,720,80)
        elseif weaponSelected == 2 then
            sprite(asset.documents.Dropbox.blasterA_png,990,720,80)
        elseif weaponSelected == 3 then
            sprite(asset.documents.Dropbox.blasterG_png,990,720,80)
        elseif weaponSelected == 4 then
            sprite(asset.documents.Dropbox.blasterE_png,990,720,80)
        end
    end
    
    function countdown(s)
        if s~=nil then
            et=ElapsedTime+s
            shoot=true
        end
        if et-0<ElapsedTime then
            shoot=false
        end
    end
    
    function accumulate(c)
        if c~=nil then
            et2=ElapsedTime+c
            spawn=true
        end
        if et2-0<ElapsedTime then
            spawn=false
        end
    end
    
    function crateSpawning(e)
        if e ~= nil then
            et3 = ElapsedTime + e
            crateSpawn = false
        end
        if et3 < ElapsedTime then
            crateSpawn = true
            createCrates(vec3(math.random(-3,3), 2, math.random(-2,2)))
        end
    end
    
  • Posts: 150

    Here’s the third last chunk of the code:

    function touched(touch)
        if touch.state == BEGAN or touch.state == MOVING then
            b.friction = 0.4
        end
    
        if touch.state == BEGAN then
            if touch.x > 870 and touch.x < 1025 then
                if touch.y > 60 and touch.y < 210 then
                        shooting = true
                        if shielding == true then
                            shieldingAction = true
                            if shieldingAction == true then
                                sound(asset.downloaded.A_Hero_s_Quest.Hit_2)
                                shield.position = bean.position + -shield.forward * 0.228 * 3.0
                            end
                        end
                    end
            end
    
            if touch.state == BEGAN then
                if touch.x > 880 and touch.x < 1035 then
                    if touch.y > 680 and touch.y < 775 then
                        if weaponDeployed == true then
                            sound(asset.downloaded.Game_Sounds_One.Zapper_1)
                            weaponSelected = weaponSelected + 1
                            if weaponSelected == 5 then
                                weaponSelected = 1 
                            end
                            for b=#weaponry,1,-1 do
                                if weaponSelected == 1 then
                                    weaponry[b].model = weapons.sword
                                end
                                if weaponSelected == 2 then
                                    weaponry[b].model = weapons.blaster
                                end 
                                if weaponSelected == 3 then
                                    weaponry[b].model = weapons.pistol
                                end
                                if weaponSelected == 4 then
                                    weaponry[b].model = weapons.sniper
                                end
                            end 
                        end
                    end
                end
            end
            if done == true then
            if touch.x > 870 and touch.x < 1025 then
                if touch.y > 550 and touch.y < 630 then
                    restartButton = true
                    notDead = true
                    dead = false
                    for h=1,#hearts do
                        heartOne = tween(1, hearts[h].heart, {position = vec3(0, -.6, 0)}, tween.easing.cubicInOut)
                        heartTwo = tween(1, hearts[h].heart, {position = vec3(0, -.85, 0)}, {easing = tween.easing.cubicInOut, loop = tween.loop.pingpong})
                        tween.sequence(heartOne, heartTwo)
                    end
                    if notDead == true and dead == false then
                        cSThree = tween(2.5, cS, {x = 2000, y = 2000}, tween.easing.linear, function() done2 = true done = false for i=1,1 do table.insert(hearts, createHearts(vec3(0, -.85, 0))) end startHeartCollision = true if startHeartCollision == true then checkHeartCollisionWithBean() end end)
                        cSFour = tween(1, cS, {x = 0, y = 0}, tween.easing.linear)
                        tween.sequence(cSThree, cSFour) 
                    end
                end
            end
        end  
            if touch.x > 870 and touch.x < 1025 then
                if touch.y > 350 and touch.y < 430 then
                    shieldingButton = true
                    if ammo <= 10 then
                        shielding = not shielding
                        if shielding == true then
                            sound(asset.downloaded.A_Hero_s_Quest.Swing_1)
                            for b=#weaponry,1,-1 do
                                weaponry[b].position = vec3(10, 10, 10)
                                shield.position = bean.position + -shield.forward * 0.208 * 2.0
                            end
                        else
                            sound(asset.downloaded.A_Hero_s_Quest.Swing_3)
                            for b=#weaponry,1,-1 do
                                weaponry[b].position = bean.position + -weaponry[b].forward * 0.245 * 2.0
                                shield.position = vec3(10, 10, 10)
                            end
                        end
                    end
                end
            end
        end
    
        if touch.state == ENDED then
            shooting = false
            followingButton = false
            shieldingButton = false
            restartButton = false
            b.type = DYNAMIC
            if shielding == true and shieldingAction == true then
                shieldingAction = false
                for i=#bobs,1,-1 do
                    velocity3 = bean.position - bobs[i].sphere1.position
                    b.linearVelocity = -velocity3*10
                end
            end
        end
    
        if touch.tapCount == 2 then
        if touch.state == BEGAN then
            if musicPlaying == false then
    
            end
            musicPlaying = true
            end
        end
    
        if touch.x > WIDTH/2 then
            if touch.tapCount == 2 then
                if touch.state == BEGAN then
                    if weaponDeployed == false then
                    for i=1,sp do
                        bc = sp
                        table.insert(bobs, createFirstSphere(vec3(math.random(-3,3),0
                        ,-1)))
                            for i=1,1 do
                                for b=#bobs,1,-1 do
                                    table.insert(spikes, spikeGen(vec3(bobs[b].sphere1.x, -2.35, bobs[b].sphere1.z)))
                                end
                            end
                        end
                    end
                end
            end
        end
    
  • Posts: 150

    Here’s the second last big chunk of code:

    if touch.x < WIDTH/2 then
            if touch.state == ENDED then
                touches[touch.id] = nil
            else
                touches[touch.id] = touch
            end
    
            for b=#weaponry,1,-1 do
                for k,t in pairs(touches) do
                    math.randomseed(touch.id)
                    if reload == false then
                        weaponry[b].rotation = quat.eulerAngles(-.012, t.pos.x + t.pos.y, 0)
                        if not shieldingAction then
                            shield.rotation = quat.eulerAngles(0, t.pos.x + t.pos.y, 0)
                        end
    
                    end
                end
            end
    
            if equipped == true then
            if reload == false then
            if touch.state == MOVING and touch.deltaY > 0 then
                b:applyForce(vec3(0, 0, 5.5))  
                pm = true
            end 
            if touch.state == MOVING and touch.deltaY < 0 then
                b:applyForce(vec3(0, 0, -5.5))
                pm = true
            end
            if touch.state == MOVING and touch.deltaX > 0 then
                b:applyForce(vec3(-5.5, 0, 0))
                if br <= 25 then
                    br = br + 5  
                end
                pm = true
            end
            if touch.state == MOVING and touch.deltaX < 0 then
                b:applyForce(vec3(5.5, 0, 0))
                if br >= -25 then
                    br = br - 5
                end
                pm = true
                end
            end
                if touch.deltaX > 0 then
                weaponRot = weaponRot + 4
            end
            if touch.deltaX < 0 then
                weaponRot = weaponRot - 4
            end
        end
        end
    
    
        if touch.x > WIDTH/2 then
            if touch.state == BEGAN then
                if touch.x > 870 and touch.x < 1025 then
                    if touch.y > 60 and touch.y < 210 then
                        if not shieldingAction and not shielding then
                        if dead == false then
                            if equipped == true then
                                if ammo ~= 0 and reload == false then
                                    -- Tap to shoot
                                    countdown(0.0125)
                                    sound(asset.downloaded.Game_Sounds_One.Pistol)
                                    recoil = true
                                    ammo = ammo - 1
                                    elseif ammo == 0 and reload == false then
                                        reload = true
                                        sound(asset.downloaded.Game_Sounds_One.Reload_1)
                                        for b=#weaponry,1,-1 do
                                            reloadOne = tween(0.34, weaponry[b], {position = bean.position + -weaponry[b].forward * 0.205 * 2.0}, tween.easing.cubicIn)
                                            reloadTwo = tween(0.34, weaponry[b], {position = bean.position + -weaponry[b].forward * 0.245 * 2.0}, tween.easing.cubicIn, function() reload = false ammo = 25 end)
                                            tween.sequence(reloadOne, reloadTwo)
                                            end
                                        end
                                    else
                                        countdown(0.0125)
                                        ammo = ammo - 1
    
                                    end
                                end
                            end
                        end  
                    end
                end
    
            if touch.state == MOVING and touch.deltaX > 0 then
                cr = cr + 2
                pm = true
            end
            if touch.state == MOVING and touch.deltaX < 0 then
                cr = cr - 2
                pm = true
            end
        end
    
        if touch.tapCount == 2 then
                if touch.state == BEGAN then
                if weaponDeployed == false then
                    sound(SOUND_JUMP, 3209)
                    sound(asset.documents.Dropbox.begin)
                    weaponGen()
                end
                weaponDeployed = true
    
    
    
                if bc == 1 then
                    print("There is "..bc.." Bob in the game!")
                else
                    print("There are "..bc.." Bobs in the game!")
                end
            end 
        end
    
        if touch.state == ENDED then
            pm = false
            b.angularVelocity = vec3(0, 0, 0)
            if swingshot == true then
                bVelocity = bean.position - lob.position
                b.linearVelocity = -bVelocity * 4.5
                b.linearDamping = 0.4
                b.friction = 2
            end
        end
    end
    
  • Posts: 150

    And finally, here’s the rest of the code:

    function checkY()
        if health <= 0 then
            dead = true
        end
    end
    
    function checkWeaponCollision()
        for b=#weaponry,1,-1 do
            x = math.abs(bean.x - weaponry[b].x)
            y = math.abs(bean.y - weaponry[b].y)
            z = math.abs(bean.z - weaponry[b].z)
            if x<=.2125 and z<=.2125 then
                equipped = true
                sound(SOUND_PICKUP, 20447)
            end
        end
    end
    
    function checkWeaponCollisionWithBobs()
        if equipped == true then
            if shoot == false then
                for a,b in pairs(bobs) do
                    x2 = math.abs(bullet.x - bobs[a].sphere1.x)
                    y2 = math.abs(bullet.y - bobs[a].sphere1.y)
                    z2 = math.abs(bullet.z - bobs[a].sphere1.z)
                    if x2<.2125 and y2<.2125 and z2<.2125 then
                        bobs[a].sphere1:destroy()
                        table.remove(bobs, a)
                        shoot = true
                    end
                end    
            end
        end
    end
    
    function checkBobCollisionWithBobs()
        if equipped == true then
            for a=#bobs,1,-1 do
                x10 = math.abs(bobs[a].sphere1.x - bobs[a].sphere1.x)
                y10 = math.abs(bobs[a].sphere1.y - bobs[a].sphere1.y)
                z10 = math.abs(bobs[a].sphere1.z - bobs[a].sphere1.z)
                if x10<.04 and y10<.04 and z10<.04 then
                    print("a bob hit anither bob!")
                end
            end
        end
    end
    
    -- In function setup()
    
    function checkCollision(c)
        if not dead then
            for i=#bobs,1,-1 do  -- parse table in reverse order to avoid remove problems
                x6 = math.abs(bobs[i].sphere1.x - bean.x)
                y6 = math.abs(bobs[i].sphere1.y - bean.y)
                z6 = math.abs(bobs[i].sphere1.z - bean.z)
                if x6<.5 and z6<.5 then
                    sound(SOUND_HIT, 20445)
                    print("hit")
                    health = health - 1
                    if health == 0 then
                        dead = true
                        deathAnim = false
                    end
                end        
            end
        end
    end
    
    function CheckCrateCollisionWithBean()
        for a=#crates,1,-1 do
            x7 = math.abs(bean.x - crates[a].crate.x)
            y7 = math.abs(bean.y - crates[a].crate.y)
            z7 = math.abs(bean.z - crates[a].crate.z)
            if x7<.3 and y7<.2125 and z7<.3 then
                print("A crate has hit the ground")
            end
        end
    end
    
    function checkSpikeCollisionWithBean()
        for a,b in pairs(spikes) do
            x = math.abs(bean.x - spikes[a].spike.x)
            y = math.abs(bean.y - spikes[a].spike.y)
            z = math.abs(bean.z - spikes[a].spike.z)
            if x<.745 and y<.745 and z<.745 then
                print("hit")
                sound(SOUND_HIT, 14244)
                health = health - 1
            end
        end
    end
    
    function checkShieldCollisionWithBobs()
        for a,b in pairs(bobs) do
            x8 = math.abs(shield.x - bobs[a].sphere1.x)
            y8 = math.abs(shield.y - bobs[a].sphere1.y)
            z8 = math.abs(shield.z - bobs[a].sphere1.z)
            if x8<.16 and y8<.16 and z8<.16 then
                print("Bob hit the player's shield!")
            end
        end
    end
    
    function checkHeartCollisionWithBean()
        for a,b in pairs(hearts) do
            x9 = math.abs(hearts[a].heart.x - bean.x)
            y9 = math.abs(hearts[a].heart.y - bean.y)
            z9 = math.abs(hearts[a].heart.z - bean.z)
            if x9<.5 and y9<.5 and z9<.5 then
                sound(SOUND_PICKUP, 42633)
                print("Player health now fully regenerated!")
                health = 500
                ammo = 25
                hearts[a].heart:destroy()
                table.remove(hearts, a)
                sp = 1
                wave = 1
                for i=1,0 + sp do
                    bc = sp
                    table.insert(bobs, createFirstSphere(vec3(math.random(-3,3),0
                    ,-1)))
                end
            end
        end
    end
    
    function checkCrateCollisionWithBean()
        for a,b in pairs(crates) do
            x10 = math.abs(hearts[a].heart.x - bean.x)
            y10 = math.abs(hearts[a].heart.y - bean.y)
            z10 = math.abs(hearts[a].heart.z - bean.z)
            if x10<.4 and z10<.4 then
                sound(SOUND_PICKUP, 42633)
                print("Player health now fully regenerated!")
                crates[a].crate:destroy()
                table.remove(crates, a)
            end
        end
    end
    
    
    function makeFire(p, e)
        fire = scene:entity()
        fire.model = craft.model(asset.builtin.Primitives.Plane)
        fire.position = p
        fire.eulerAngles = e
        fire.scale = vec3(1.4, 1.4, 1.4) / 8
        fire.material = craft.material(asset.documents.Dropbox.Stylised_Fire)
    end
    
    function createFirstSphere(p)
        sphere1=scene:entity()
        s1=sphere1:add(craft.rigidbody,DYNAMIC)
        s1.sleepingAllowed=false
        sphere1.name="Red Enemy"
        sphere1.position=p
        sphere1.model = craft.model.icosphere(1,5)
        sphere1:add(craft.shape.sphere, 1)
        sphere1.scale = vec3(2, 2, 2)/8
        sphere1.material = craft.material(asset.builtin.Materials.Specular)
        sphere1.material.diffuse=color(255,0,0)
        return({sphere1=sphere1,ss=s1,s=scale})
    end
    
    function createFirstCapsule(p)
        capsule1=scene:entity()
        c1=capsule1:add(craft.rigidbody,DYNAMIC)
        c1.sleepingAllowed=false
        capsule1.name="Green Enemy"
        capsule1.position=p
        capsule1.model = craft.model(asset.builtin.Primitives.Capsule)
        capsule1:add(craft.shape.capsule, 1)
        capsule1.scale=vec3(1, 1, 1) / 8
        capsule1.material = craft.material(asset.documents.Dropbox.Glow)
        capsule1.material.diffuse=color(0,255,0)
        return({sphere1=capsule1,ss=c1,s=scale})
    end
    
  • Posts: 150

    Also, some of the assets that I use are not built in.

  • dave1707dave1707 Mod
    Posts: 9,725

    @Creator27 I just skimmed thru your code not really looking for anything. Something you might want to do. Put/copy your assets into the project folder, that way they’re all in the same place. Then you can export everything as a zip file and post the zip file to the forum. That way when the zip file is used, all the assets are there and everything runs fine without having missing assets.

    Also, so everyone doesn’t have to search thru your code, tell us where you spawn the entities and where the for loop is that’s giving you the problem.

    If you’re unsure how to put assets into the project folder or zip the project, let us know.

  • dave1707dave1707 Mod
    Posts: 9,725

    @Creator27 Your code is too hard to get to run the way it is. Even after replacing models and sprites with anything that works, what are you supposed to do and where do you tap to do anything.

  • Posts: 150

    @dave1707, just to spare you the pain of looking over my entire code, I’ll just give you the code that I think is the problem, cannot confirm if it is.

    table.insert(bobs, createFirstSphere(vec3(math.random(-3,3),0
    ,-1)))
    
  • Posts: 1,275

    math.random(-3,3) only returns -3, -2, -1, 0, 1, 2, or 3. you'll never see more than 7 spheres. could that be the issue?

  • Posts: 150

    @RonJeffries, I did play around with the numbers and sure enough, that was actually apart of the issue. The only thing is, that whatever I tried to put in just didn’t work, and in some cases made the situation worse.

  • Posts: 1,275

    that is possible. but no one can generally help with a 1000 line program. one needs to come up with small programs showing the problem.

  • Posts: 150

    @RonJeffries, I understand. I will make a quick example to show the problem.

  • edited October 5 Posts: 150

    @RonJeffriesm here it is:

    Fixing
    
    function setup()
        -- Create a new craft scene
        scene = craft.scene()
        -- This is the table that I insert the spheres into
        bobs = {} -- Don't judge the name :)
        scene.camera.z = -8
        -- This number will increase by 1 every time you touch the screen
        sp = 1
    end
    
    function update(dt)
        -- Update the scene (physics, transforms etc)
        scene:update(dt)
    end
    
    function touched(touch)
        if touch.state == BEGAN then
            for i=1,sp do
                table.insert(bobs, createFirstSphere(vec3(math.random(-1,1),0 -- If you change the math.random numbers to something different, that's the problem.
                ,-1)))
            end
        end
    end
    
    function createFirstSphere(p)
        sphere1=scene:entity()
        s1=sphere1:add(craft.rigidbody,DYNAMIC)
        s1.sleepingAllowed=false
        sphere1.name="Red Enemy"
        sphere1.position=p
        sphere1.model = craft.model.icosphere(1,5)
        sphere1:add(craft.shape.sphere, 1)
        sphere1.scale = vec3(2, 2, 2)/8
        sphere1.material = craft.material(asset.builtin.Materials.Specular)
        sphere1.material.diffuse=color(255,0,0)
        return({sphere1=sphere1,ss=s1,s=scale})
    end
    
    -- Called automatically by codea 
    function draw()
        update(DeltaTime)
    
        -- Draw the scene
        scene:draw()    
    end
    
  • dave1707dave1707 Mod
    Posts: 9,725

    @Creator27 If you’re trying to create spheres randomly on a surface, maybe you should have a random position for x and a random position for z instead of just -1 for z. As @RonJeffries pointed out, the way you’re doing it now, you can only see a max of 7 spheres. By adding the random position for z, you can see a max of 49 spheres.

    table.insert(bobs, createFirstSphere(vec3(math.random(-3,3),0,math.random(-3,3)))
    
  • Posts: 150

    @dave1707, I can’t tell if your code worked because of this: in my game, I have a number that defaults to 1. For my game, you use raycasting to destroy red spheres. If there are no red spheres left, the number I mentioned earlier increases by 1 and depending on what number it is, that is how many new red spheres will spawn. But for some weird reason, whenever I destroy two red spheres, no more spawn and I can’t seem to wrap my head around why.

  • dave1707dave1707 Mod
    edited October 6 Posts: 9,725

    @Creator27 Maybe it’s time to put print statements in your code to figure out what’s happening with the createFirstSphere function. Just remember, print statement can mess things up if they print constantly.

    Sometimes I’ll put stop() in the code after the print statement. Since the function stop() doesn’t exist, the code stops with an error, but you can see what the print shows. If you need more print statements, you can create an actual stop function that increments a counter. If the counter exceeds a set value, then call a non existent function to cause an error. Then you can look at how many print statements you wanted.

  • edited October 6 Posts: 150

    @dave1707, I’m a bit confused at what you mean, as I’m not an extremely skilled programmer and have absolutely no idea of how to fix conditional loop problems.

  • dave1707dave1707 Mod
    Posts: 9,725

    @Creator27 I was suggesting that you put a print statement in the function createFirstSphere to see what values it getting and if it’s being called when you think it is. But the problem with print statements is, if they’re in the wrong place, they’ll print forever and eventually crash the code.

  • Posts: 2,496
    @Creator27 - I often use print statements in a function called from a key routine (even sometimes draw() and usually operated when a Boolean parameter is set). Also, don't forget to use output.clear() unless you want tomonitor change then writing to a tab is better
  • Posts: 150

    @Bri_G, are you able to give me a brief rundown on what exactly print statements are?

  • dave1707dave1707 Mod
    Posts: 9,725

    @Creator27 Why are you asking “what exactly print statements are”. I just looked thru your code and you have several print statement in there now. So apparently you know what print statement are.

    I’m able to somewhat run your code, but I haven’t the slightest idea of what’s supposed to happen or how to do anything. That kind of hinders trying to find what problems you’re having.

  • Posts: 150

    @dave1707, yeah I think you’re probably right about that. Apologies if I got a bit annoying :)

  • Posts: 150

    @dave1707, in the project you move the capsule (the player) around using your finger on the left side of the screen. If your finger moves across to the other half of your screen then the player will stop moving. If you double tap on the right side of your screen, a red sphere will appear to try to eliminate you. In order to stop it from doing that, you must press the explosion button to shoot but be warned as you only have 25 bullets to shoot before you have to reload in which you tap the shoot button again. Also, sorry if the shooting doesn’t work well, I will try to get to that. If you kill the red sphere, another one will spawn and so on and so on. But when you kill the very second red sphere that spawns, that’s where the problem is.

  • dave1707dave1707 Mod
    Posts: 9,725

    @Creator27 Another problem with running your code are the assets. Because you’re not using builtin assets, when you post your code, none of your assets are included and kick out as errors when the code is run. Built in assets can be used to get the code to run, but then nothing makes any sense because none of the sprites match anything. So the code is almost useless to try to run let alone try to debug. So that’s why no one is coming up with a solution to your problem.

  • Posts: 1,275

    @Creator27 I am interested in trying to help you, not so much with your current specific problem, but with learning how to build a program like this one, so that you, and people who might help you, can understand it, test it, make fewer mistakes, and debug it when it doesn't work.

    Doing that would entail a lot of work on my part, and even more on yours. Would such a thing be interesting to you?

  • dave1707dave1707 Mod
    Posts: 9,725

    @RonJeffries Would you be doing that in this forum. That might be interesting to a lot of other people, including me. I’m more of a hacker programmer and don’t really follow any kind of rules. I just code until it works and if it’s something that I want share or just keep for myself, I’ll go thru the code and try to make it more compact and readable.

  • Posts: 1,275

    I'm not sure how best to do it. It seems to me that @Creator27 may have very little programming experience—and I am quite impressed with how much they have done if that's the case—so it might have to start pretty basic. I'm not sure if it fits the forum style, or what. Usually, of course, I'd start a category on my web site for it.

    Let's kick around some possibilities? What do you think would be ideal?

  • dave1707dave1707 Mod
    Posts: 9,725
    @RonJeffries Maybe on your site would be better. Just wasn’t sure how you were going to do it, how basic it would be, or if it would be open to discussion/questions.
  • Posts: 1,275

    One issue with my site is that I don't allow comments. That is something to take into consideration. Maybe articles there and questions here?

  • dave1707dave1707 Mod
    Posts: 9,725

    @RonJeffries That would probably work OK.

  • Posts: 2,496

    @RonJeffries - it's worked before so probably will again. I'd like to see your approach.

  • Posts: 1,275

    well just a random notion at the moment, not sure how valuable it would be.

  • edited October 7 Posts: 150

    @RonJeffries, two things. Yes you are right, I don’t have a lot of programming language though I have dabbled in C# and am somewhat fluent in Lua. And I know this might be annoying to hear, but I’ve scrapped the idea anyways and I’m working on a different sort-of idea in the game. Apologies for not telling you earlier!

  • Posts: 1,275

    ok

  • My take away from this is that the forum has several members who are rather skilled and willing to create examples to help noobs and pro hacker coders alike on the “proper standards” of coding. I too would be interested in helping with that endeavor.

    @Creator27 the main thing to take from this string for you personally is that we are willing to help. The way other programmers can help is by having a dedicated example program that grows as a demonstration. Preferably in a manner that can be useful for all who come after. If you so desire such help personally, you need only ask, and to be willing to see said example project to the end.

    Man I love this forum
Sign In or Register to comment.