Frustrations

I have been playing with Codea for a few weeks now, and I’m really having a hard time wrapping my brain around the language. I will admit that I am not a programmer, but have dabbled, and want to learn. I have been reading the references, example codes (learned some things through manipulating them), and have done the tutorials. I have done my own coding and was able to draw an ellipse and square. I even applied some gravity to the shapes to make them move according to the tilt of the iPad. I just don’t know where to go from there. This is not like GML (I think that is object based programming?), and I feel stuck.

Sorry for the venting. I think Codea is really neat. People can do super cool things with it. I just wish I understood it more, and that there was better explanation why you use certain code to do certain things. I will keep plugging along.

It took me a little bit too. What really helped is that I started to really get in to Python at the exact same time as Codea.

Both are similar in some senses. There are a few quarks in both that can throw you off if you arn’t paying attention. Yes this can do object oriented stuff too. In fact, many of the Object Oriented Principles I learned in Codea I was able to use to apply right in to Python. Before that, I had no concept of creating Objects and Instances, etc.

The best advice I can give is to keep working at it. Start basic and move forward from there. It helps to have something that you want to work toward or have an idea. Then struggle your way until you reach your goal. By the time that all happens, you will feel confident in all the things that were once so hard, but now they are easier.

Best of luck and keep with it!

@LadyJayne applying gravity to some shapes was an excellent way to start, and I think tackling some more simple ideas will help you move into more advanced areas.

You could try working on the following ideas

  • Making an ellipse move under your finger using CurrentTouch.x and CurrentTouch.y

  • Making two ellipses move by responding to the touched( touch ) function callback (to handle multiple touches)

  • Create a class that draws a house and can be parameterised by its position and size

  • Use the house class above to draw fifty houses on a green landscape with a blue sky

I wonder if some other forum members can think of some simple exercises that explain simple concepts?

Also please feel free to post specific questions to this forum when you get stuck, no matter how basic. I am more than happy to assist when you run into problems and I’m sure others will, as well.

Thanks for the encouragement and ideas. I will take Simeon’s suggested projects and go from there. :slight_smile:

Feel free to post your progress as you go in this thread.

I think the best way is to just keep playing around with it. I’ve created a few silly programs, and at first I was a bit frustrated that it wasn’t similar to moai that I used for a game recently, but now I’m used to Codea and the api works fine, with just some minor issues.

Also, remember that YOU made the ellipse and square move. Sure it’s not going to impress your friends, but using something like game maker does a lot of the stuff for you and means that you don’t actually learn how things work. Stick with hacking out some (very) small projects and it won’t be long before you find tools like gamemaker limiting.

@LadyJayne, I think the most important question is – what do you want to do? Is there a project you have in mind?

One you have that target, you can break it down into a series of tasks and knock them off one at a time.

The best way to eat an elephant is one bite at the time :slight_smile:

Second bite of the elephant (already made the ellipse move using CurrentTouch) :slight_smile:
Make ellipses move using touched(touch):


-- Use this function to perform your initial setup
function setup()
    print("Multi-touch spots")
    touches = {}
end

function touched(touch)
    if touch.state == ENDED then --if touches have ended
        touches[touch.id] = nil --clear the 'touches' table
        else
            touches[touch.id] = touch --else, fill 'touches' table, 
            --and assign touch IDs to variable 'touch'
    end
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(19, 19, 228, 255)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    for k,touch in pairs(touches) do --look at the key-value in the table
        ellipse(touch.x,touch.y, 75) --draw an ellipse for each ID at the x and y of the touch
    end
end

I commented out what I thought the code was doing. I still don’t understand how/why of “in pairs”. I was just following examples of multi-touch. Can anyone explain? Thanks again. I really appreciate the help.

Touches is a table - “for k,touch in pairs(touches)” assigns k to the key and touch to the value of each table entry in turn.

Example - if my table was { a,b,c,d,e,f }, then first time thru the loop k would be “a”, and touch would be “b”, then the next time you’d get “c” and “d”, and so on.

What you get above is “for each touch in my table of touches, go do this”.

I feel your frustration at times, LadyJayne. Just a newbie here with Codea but I am learning every day small steps at a time.

It does help as mentioned to have somewhat of a goal in mind and reach it in small increments. Good luck.

I feel I may be making progress (even if it took me a few hours and more reading for such a simple code). Here is my house, paramaterized:


-- Use this function to perform your initial setup
function setup()
    print("Location, Location, Location!")
   
  
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    House:draw()

end


House = class()
    parameter("Width", 10,200,110)
    parameter("Height", 10,200,78)
    parameter("X", 25, 500,WIDTH/2)
    parameter("Y", 25,500,HEIGHT/2)
 

function House:init(x)
    -- you can accept and set parameters here
    self.x = x
   
end

function House:draw()
    -- Codea does not automatically call this method
    
        sprite("Small World:House",X,Y,Width,Height)
        
end

function House:touched(touch)
    -- Codea does not automatically call this method
end

What is the purpose for self.x = x? I think it is suppose to be the x coordinate, but it seems unneeded with the parameters. Is the init() in class like the setup() is in main? Thanks.

@LadyJayne self.x = x is just an example line showing how you can accept arguments in the constructor to set member variables.

There’s a downside to using parameter sliders to control those aspects of your house — those parameters will be set for all houses you make, and you won’t be able to vary the size of individual houses if you make more than one instance.

Here’s how you could create the class to allow for individual properties on each house.

House = class()

function House:init()
    -- Create some members that will control your house

    self.position = vec2( 0, 0 )
    self.width = 50
end

function House:draw()
    sprite("Small World:House", self.position.x, self.position.y, self.width)        
end

If you construct it in this way, you can use it as follows:

function setup()
    house1 = House()
    house2 = House()

    house1.position = vec2( WIDTH/2 - 50, HEIGHT/2 )
    house1.width = 30

    house2.position = vec2( WIDTH/2 + 50, HEIGHT/2 )
    house2.width = 60
end

function draw()
    background(0,0,0)

    house1:draw()
    house2:draw()
end

As you can see, each house can have its own position and width (size).

Ok. Thanks. Do I HAVE to have:

function House:init()
    -- Create some members that will control your house

    self.position = vec2( 0, 0 )
    self.width = 50
end

After typing it in, and playing around with the code, I commented out the self.position and self.width, and it ran the same way. I suppose it would save time to set the first house values here, and I wouldn’t need to code house1.position or house1.width in main().

Am I correct in this logic?

I am now working on getting 50 houses on the screen. I figured out how to display 50 houses randomly every frame. I just haven’t figured out how to get the 50 houses to stay in one place after they are drawn. I’ll continue to work on that before I post my code.

Thanks again for your help!

The reason for having them there in the first place is to assign some default values — this is in case you do not assign values to your house’s position and width later.

For example, when you draw the house, it will attempt to use self.position.x — if this value is not defined then your program will generate an error. Having the values set to defaults in the constructor is a way to safeguard against this.

I felt like writing a version of this. It generates random houses, and places them randomly on hills. Here’s a picture

Random Houses

Here’s the code http://pastebin.com/MULuA8Zm

Thanks for this @Simeon a great example with lots of other topics within for us to learn from. :slight_smile:

Wow! OK. Going to type in the code and play around with it.
I may keep working on figuring out how to create a table to fill with houses, and then get that table to draw. I’m sure the above code will help with that too.

Quick question on Simeon’s code: In the Hill class, function Hill:draw(), you have a for loop:

for _,v in pairs(self.houses) do
v:draw()

What is the ‘_,’?