Metatable query

Is there a way to make your own “states”
Like touch.state can be ENDED, BEGAN, or MOVING; can you make your own states? Or make an equivalency between “screen” and “GAME” so I could check it like “if screen == GAME”? If this isn’t a possible thing, then it totally needs to be.

Of course. ENDED, BEGAN, and MOVING are just variables with a number assigned.

@Monkeyman32123 - try this

http://coolcodea.wordpress.com/2013/05/04/47-game-template/

@JakAttak well now don’t I feel silly? :stuck_out_tongue:

@Monkwyman32123 If I understand your question correctly, I created a scene class that can handle different game states. You simple create a new class for each scene and register it. The different game scenes can persist or reset when they are called again.

http://twolivesleft.com/Codea/CC/alpha/index.php?v=1551

I have a game called ‘Tilt Island’, It should be on CC, but it uses this example for states:

Easy = 1
Hard = 2
Med = 3
state = Easy
function setup()
if state == Easy then
--Do Something
elseif
state == Med then
--Do something else
elseif state == Hard then
--Do a last thing
end
end
end
end

Why does this not work to get rid of useless lasers in the lasers table? The laser objects have a variable called alive that automatically turns false when they become useless

local b = 0
    while a == false do
        for i= 0, #lasers do
            if lasers[i] ~= nil and lasers[i].alive == false then
                table.remove(lasers, i)
                b = b + 1
            end
        end
        if b > 0 then 
            b = 0
        else 
            a = true
        end
    end

@Monkeyman32123 - loop backwards, ie

for i=#lasers,0,-1 do

and that should work. (Assuming it does), see if you can figure out why the other way didn’t.

It didn’t work >_>

@Monkeyman32123 - actually, you might be able to reduce it to

    for i= #lasers,0,-1 do
         if lasers[i].alive == false then table.remove(lasers, i) end
    end

Ok, the reduced one works, thank you @ignatz (even if I don’t understand the wizardry behind it)

@Monkeyman32123 - the reason it didn’t work before can be seen with an example.

Suppose items 4 and 5 were false.

You loop through, starting from 0, and when you get to 4, you see it is false, so you delete it from the table. Unfortunately, this means item 5 now becomes item 4, and item 6 becomes item 5.

So when you carry on and I becomes 5, you are actually looking at the item that was 6 originally, and you have skipped looking at the original item 5.

If you work backwards, you avoid that problem.

That’s why it was in a while loop, I understand that you skip looking at some and the while loop was to make it go through multiple times until it never found a false, but I don’t understand why my much more complex and roundabout way didn’t end up working at all

@Monkeyman32123 - you needed to reset b=0 each time you started the while loop, ie move it down one line

Okay- so…I have supportedOrientations(LANDSCAPE_ANY) but when starting the viewer it will still begin in portrait if codea is currently in portrait. Is there a way to force it to start in LANDSCAPE? or at least force it to change to landscape, or even better, to actually stop it from starting in landscape? Is this a problem that would fix itself once published to the App Store?

@Monkeyman32123 Do you have your supportedOrientation as one of the first lines of code before, not in, the setup() function.

@dave1707 Nope, but I do now, and it fixed it, so, thanks. And now I feel silly

@Monkeyman32123 There’s no reason to feel silly about learning something new. I did the same thing when I started with Codea. If you go back in the forum far enough, you’ll find my post about the same thing.

Haha, well, that makes me feel better
I’m pretty new to coding, and considering I took on a full scale project as my first project, I’ve felt silly quite a few times now XD

How do I make classes inherit from other classes in lua? Like say I had an animal class that defined he basic properties for animals, then I wanted to make a class (let’s say horse) that inherits the functions of the animal class but can have its own special functions and such