Many bugs. I don't know where to start. Please help

For some reason, every single time I run this, it never works. I’m new to Corona, and I’ve been encountering bugs for QUITE some time. So much so, that it has halted any progress I’ve made. Every single time I attempt to fix one bug, another pops up, which doesn’t help. 

Here’s the code (it’s for a game):

         

display.setStatusBar(display.HiddenStatusBar)

local physics = require “physics”

physics.start()

physics.setGravity(0,0)

local pWidth, pHeight = display.actualContentWidth, display.actualContentHeight

– create wall objects

local topWall = display.newRect( 0, 0, display.contentWidth+20, 20 )

local bottomWall = display.newRect( 0, display.contentHeight - 10, display.contentWidth, 20 )

local leftWall = display.newRect( 0, 0, 10, display.contentHeight )

local rightWall = display.newRect( display.contentWidth - 10, 0, 10, display.contentHeight+20 )

– make them physics bodies

physics.addBody(topWall, “static”, {density = 1.0, friction = 1, bounce = 40, isSensor = false})

physics.addBody(bottomWall, “static”, {density = 1.0, friction = 1, bounce = 40, isSensor = false})

physics.addBody(leftWall, “static”, {density = 1.0, friction = 1, bounce = 40, isSensor = false})

physics.addBody(rightWall, “static”, {density = 1.0, friction = 1, bounce = 40, isSensor = false})

local background = display.newImage(“background.jpg”)

local happy = display.newImage(“happy.png”)

happy.x = 100

happy.y = 100

happy.id = “happy”;

physics.addBody(happy, “dynamic”)

– TOUCH EVENTS FOR HAPPY MOVEMENT

function touchScreen(event)

        if event.phase == “ended” then

                transition.to(happy,{time=700, x=event.x, y=event.y})

        end

end

Runtime:addEventListener(“touch”, touchScreen)

– ok shlok, trying to make collisions work anything after this if it doesn’t work u can delete

 local backgroundmusic = audio.loadStream("_ElecTechno.m4a")

local backgroundmusic2 = audio.play( backgroundmusic, { loops=-1, fadein=2000 } )

– every x seconds

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 spawnzombie()

    local zombie = display.newImageRect(“zombie.jpg”, 45, 45);

    zombie.x = 200;

    zombie.y = 200;

    function movezombie()

    transition.to(zombie,{time=7000, x=math.random(0,500), y=math.random(0,600), onComplete=movezombie});

end

    physics.addBody(zombie, “dynamic”, {density = 0.1, bounce = 0.1, friction = .1, radius = 0});

    movezombie()

    --Adding touch event

    zombie:addEventListener(“touch”, zombie);

    zombie.collType = “dynamic”

physics.addBody( zombie, { bounce=10.0, friction=1.0 } )

function zombie:collision(event)

   if event.phase == “began” then

      if event.other.id == “happy” then 

         if not event.other.wasHit then

            event.other.wasHit = true;

            timer.performWithDelay(100, event.other.removeSelf, 1);

         end

      elseif event.other.id == “laser” then 

         if not self.wasHit then 

            self.wasHit = true;

            timer.performWithDelay(100, self.removeSelf, 1);

         end

      end

   end

end

zombie:addEventListener(“collision”, zombie);

end

local total_zombie = 100

tmr = timer.performWithDelay(2000, spawnzombie, total_zombie);

– BUTTON code

local widget = require( “widget” )

local function handleButtonEvent( event )

        local phase = event.phase

        if “ended” == phase then

                print( “you pressed and released a button!” )

        end

end

– guns

local function fireLasers()

  local laserbeam = display.newImage(“laserbeam.jpg”)

  laserbeam:scale( 0.5, 0.5 )

  laserbeam.x = happy.x

  laserbeam.y = happy.y

  laserbeam.id = “laser”;

  physics.addBody(happy, “dynamic”)

  transition.to(laserbeam, {time=1000, x = math.random() ,y = math.random(), onComplete=movelaserbeam});

end

local fireButton = display.newImage( “firebutton.jpg” )

fireButton.x = 50

fireButton.y = display.contentHeight-50

local function handleFireButton( event )

        if ( event.phase == “began” ) then

                fireLasers()

        elseif ( event.phase == “ended” ) then

                fireLasers()

        end

        return true

end

fireButton:addEventListener( “touch”, handleFireButton )

local needToFire = false

local function handleEnterFrame( event)

        if ( needToFire == true ) then

                fireLasers()

        end

end

Runtime:addEventListener( “enterFrame”, handleEnterFrame )

local fireButton = display.newImage( “firebutton.jpg” )

fireButton.x = 50

fireButton.y = display.contentHeight-50

local function handleFireButton( event )

                if ( event.phase == “began” ) then

                        needToFire = true

                elseif ( event.phase == “ended” and needToFire == true ) then

                needToFire = false

        end

        return true

end

fireButton:addEventListener( “touch”, handleFireButton )

– ugh

local needToFire = false

local function handleEnterFrame( event )

   if ( needToFire == true ) then

      fireLasers()

   end

end

Runtime:addEventListener( “enterFrame”, handleEnterFrame )

local function handleFireButton2( event )

   if ( event.phase == “began” ) then

      – fire the weapon

      needToFire = true

   elseif ( event.phase == “ended” and needToFire == true ) then

      – stop firing the weapon

      needToFire = false

   end

   return true

end

local fireButton2 = widget.newButton{

   width = 64,

   height = 64,

   defaultFile = “firebutton.jpg”,

   overFile = “button1.jpg”,

   onEvent = handleFireButton

}

fireButton.x = 50

— splash screen?

local splashscreen = display.newImage( “ins.png” );

local removesplashbutton  --up-value reference

local function removeSplash( event )

   splashscreen:removeSelf()

   removesplashbutton:removeSelf()

end

removesplashbutton = widget.newButton{

        width = 100,

        height = 100,

        defaultFile = “smallbutton.png”,

        overFile = “button1.jpg”,

        onPress = removeSplash,

}


Alright. So if anyone can help, that would be AMAZING. Please and thank you. 

What errors are you getting?

First of all, the function :

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

I say that it is incorrect the call to a something “strange”. You shout look to the manual:

http://docs.coronalabs.com/api/library/transition/to.html

You can call another function with its name. onComplete = AnotherFunction

Some lines down, you make a recursive function.

function movezombie()
    transition.to(zombie,{time=7000, x=math.random(0,500), y=math.random(0,600), onComplete=movezombie});

end

but I don’t know if something will stop this or it loops forever and also if it is possible to do this!

Try to work on this and then post some more errors.

Recursive function calls like movezombie calling movezombie are fine, and quite common in games. So I don’t think that’s your problem.

If I were you, I would comment out the whole code, and comment bit by bit back in. Every time, run the program and make sure there are no errors. You should always test and test and test while developing, to avoid getting into a deadlock like this where fixing one bug creates another.

So you are not getting any errors then?  Yes, recursive functions are allowed.  Code in the docs are generally snippets to give you ideas on how things can work.  Obviously you should have a way to stop the transition like storing a reference to the transition that you can then call transition.cancel() on later.

Rob

What errors are you getting?

First of all, the function :

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

I say that it is incorrect the call to a something “strange”. You shout look to the manual:

http://docs.coronalabs.com/api/library/transition/to.html

You can call another function with its name. onComplete = AnotherFunction

Some lines down, you make a recursive function.

function movezombie()
    transition.to(zombie,{time=7000, x=math.random(0,500), y=math.random(0,600), onComplete=movezombie});

end

but I don’t know if something will stop this or it loops forever and also if it is possible to do this!

Try to work on this and then post some more errors.

Recursive function calls like movezombie calling movezombie are fine, and quite common in games. So I don’t think that’s your problem.

If I were you, I would comment out the whole code, and comment bit by bit back in. Every time, run the program and make sure there are no errors. You should always test and test and test while developing, to avoid getting into a deadlock like this where fixing one bug creates another.

So you are not getting any errors then?  Yes, recursive functions are allowed.  Code in the docs are generally snippets to give you ideas on how things can work.  Obviously you should have a way to stop the transition like storing a reference to the transition that you can then call transition.cancel() on later.

Rob