attempt to index local 'newAstroid' (a nil value)

Hey Guys!

I have my app working up to where it should be in the Chapter 3 in the tutorial!  Thank you for helping!  I am at the part where the tutorial suggests trying the app so far (Action!).  Shall I post my code so that future noobs can see it?

Great news, once your done with the Getting Started guide you could add to the game.

For example, I added more random locations for Asteroids and the feature to drag the ship anywhere you want on the screen.

I’m glad you’ve discovered this. We were considering a bonus chapter with some additional tweaks. There is way more you can do.

Consider adding:

  1. A settings screen to give your users control over the sound effects and background music (on/off and/or volume)

  2. A help screen.

  3. Add difficulty to the game (set on the settings screen) that would control how fast the asteroids spin.

  4. Add a UFO. The original Atari Asteroids game had a UFO that would come on screen and shoot at you.

  5. That game also had a hyper jump option where if you were about to be hit it would jump you somewhere else on the screen. Given the volume of asteroids in StarExplorer, that might be a challenge to implement.

  6. There are three asteroids in the image sheet. Change the spawn to use a different asteroid.

  7. Add an explosion animation.

7a. If you want a real challenge, you would need to alter the image sheet or make a new one that has smaller asteroids in it and when you blow up an asteroid it turns into a bunch of smaller asteroids. You could use our particle effects library to take the smaller asteroid image and create that explosion.

  1. Add a text entry field so the player can give them a name and change the high scores table to show the persons name with the score.

What else can you think to add?

I was contemplating on a UFO, how would I add that AI function?

Hi Rob

All those things sound cool.  As soon as I get the Star Explorer game (the tutorial’s game name) complete, I plan to hack the code and images and create my own spin on the game.  I did that with the “Ballon Tap” game and got stoked with my results!  I would attach it here but don’t see a way to.

If you look click more reply options you can upload a .zip file containing your Balloon Tap game.

Even after I zipped my .apk, it was still to large to attach (3.6MB) :frowning:

There are file size limits on uploads. The purpose is to upload screen shots, not large projects. You best bet to share your APK would be to upload it to some place like Dropbox or Google Drive and share it from there. You can then paste the link to the download in the forums.

Rob

That makes sense.  Here is a screen shot of “CacoDemon Tap”  If anybody wants the .apk file, I’ll provide a link

:smiley:

Nice looking screen shot!

Definitely a bit spooky… Looks great!

Hello, im new here, and im the chapter 2! as well as a the user “beyond2k”, but I have a problem, mmm Im not sure what Im doing wrong cause…well, I take my notes, and pay attentio to the code, actually when I test it it dont give me any error, but I cant see the asteroids randomly appearing in the screen, and even the laser when Im typing the screen. I used the same code posted here about the asteroids…and nothing…what can I do?

Thank you very much guys!  That’s my love for Doom in my learning :slight_smile:

I am working on getting the rest of the tutorial finished.  I coded it til the end and it throws errors.  I have to look at it later as I have to get some work done! :slight_smile:

@beyond2k, there is one scene where we intentionally break things. Once we start with scenes, you may have to get through a couple of chapters before you can run it again.

@arellano.marco, where are you in the tutorial?

Rob

Oh, never mind :P …I read one more time this topic, and find that code that helps to spawn the asteroids…and my code in fire mechanics was wrong,  the listener was in the wrong place hahah I checked “ballon tap” and found my mistake. Thank you, anyway, This is kinda messy…

timer.performWithDelay(1000,function()

createAsteroid()

end,0)

Somebody can explain me how it works?

timer.performWithDelay(1000,function() createAsteroid() end,0)

This code will spawn an Asteroid with by calling the createAsteroid function every second and will perform this infinite times.

thank you, and what about that “end, 0)” what that means?

timer.performWithDelay is a function which calls a function every x miliseconds the 0 is the amount of loops but in this case it’s 0 so it won’t stop(anything under 1 will go on forever)

Read more here

https://docs.coronalabs.com/api/library/timer/performWithDelay.html

the ‘end’ is actually apart of the anonymous function. but after I look over the code I realised I did not need an anonymous function

timer.performWithDelay(1000,createAsteroid,0)

I think an earlier version of the tutorial was passing parameters to the createAsteroid() function which is why it’s wrapped in the anonymous function.

For the purpose of education. An anonymous function is simply an unnamed function. It’s useful to create in line functions that are only going to be used by that one command. For instance, if you look at the docs for timer.performWithDelay(), you will see the parameters are:

timer.performWithDelay( time, functionToCall, iterations )  (I’m not looking at the actual docs, the verbiage will likely be a bit different).

In this case, the timer.performWithDelay() method is expecting an “address to a function” as the second parameter. When you create a function using:

local function myFunctionName()

You are in effect creating a variable named myFunctionName that happens to hold the address to the memory location where the function’s code is stored. Lua lets you also do:

local myFunctionName = function()

     – your function code

end

which is basically creating a function somewhere in memory and storing the address of that function in myFunctionName, but in this case, its considered an anonymous function since you are not going through the formal naming process. Internally there is little difference.

All function blocks have to end with an “end”. It might be easier for you to understand that block of code presented like this:

timer.performWithDelay(     1000,     function()         createAsteroid()     end,     0 )

Here each of the three parameters (separated by commas) are on separate lines.

EDIT: I solved the problem, to anybody with same issues, just a few tips:

1.Run your code until you have your listener running!

  1. Be careful, with “if” and “elseif” you have to, always, finish them with “end”! Dont panic, just be careful! 

3- Notes! take notes all you can! Remembering you how commands, variables, functions works etc. This could be a feedback for you as you are programming, it will leave you fresh info and a better learning.

cheers!

Wow, that was easy, thank you, Rob and Lua! It was just a thing of perspective! :) Sorry to bother you, again, but im in the part of collisions, all was going fine when it give an error "attempt to index. global obj1 (a nil value) stack traceback:

main.lua:331:un main chunk. I tried, but i dont have a good understanding about what is happening. T think that is something about declare obj 1 and obj2 in order to make the global collision…but Im not really sure.

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

        local obj1 = event.object1

        local obj2 = event.object2

        

  end     

  end

   if ( ( obj1.myName == “laser” and obj2.myName == “asteroid” ) or

             ( obj1.myName == “asteroid” and obj2.myName == “laser” ) )

        then

            – Remove both the laser and asteroid

            display.remove( obj1 )

            display.remove( obj2 )

            for i = #asteroidsTable, 1, -1 do

                if ( asteroidsTable[i] == obj1 or asteroidsTable[i] == obj2 ) then

                    table.remove( asteroidsTable, i )

                    break

                end

            end

            – Increase score

            score = score + 100

            scoreText.text = "Score: " … score

        end