Fight The Boy

This is my first program so I hope you like it.

function setup()
    print("swipe on the boy to hit him.")
    function draw()
        background(0, 243, 255, 255)
        sprite("Planet Cute:Character Boy", 400, 500)
        function touched(touch)

            if touch.state==MOVING then
                sound(DATA, "ZgBAPQBGPhleSiRr8Jp1vjxX4j6CwGU+TABhWw9EOFlAO2E0")
                if touch.state==ENDED then
                    sound("A Hero's Quest:Hurt 1")
                    end
                    sound("A Hero's Quest:Hurt 2")
                    sound("A Hero's Quest:Hurt 3")
                    sound("A Hero's Quest:Hurt 5")
                    sound("A Hero's Quest:Level Up")
                print("LEVEL UP!")
                end
            end
        end
    end

@MonsterJace Welcome. Some suggestions, when you post code, put 3 ~,s on a line before and after your code so it displays on the forum correctly. I added those to your post above. Also, the functions setup(), draw(), and touched() should be seperate routines. You have draw() and touched() nested in setup(). Below is your code with those functions separated.

function setup()
    print("swipe on the boy to hit him.")
end

function draw()
    background(0, 243, 255, 255)
    sprite("Planet Cute:Character Boy", 400, 500)
end

function touched(touch)
    if touch.state==MOVING then
        sound(DATA, "ZgBAPQBGPhleSiRr8Jp1vjxX4j6CwGU+TABhWw9EOFlAO2E0")
    end
    if touch.state==ENDED then
        sound("A Hero's Quest:Hurt 1")
    end
    sound("A Hero's Quest:Hurt 2")
    sound("A Hero's Quest:Hurt 3")
    sound("A Hero's Quest:Hurt 5")
    sound("A Hero's Quest:Level Up")
    print("LEVEL UP!")
end

Good start, welcome :-bd

Welcome!

@MonsterJace, welcome to the Forums. I joined on your join date.
I would help you a little:
Use touch.x and touch.y and the music will be only if you scroll at this position

@MonsterJace, this is better:

function setup()
    print("swipe on the boy to hit him.")
    level = 1
end

function draw()
    background(0, 243, 255, 255)
    sprite("Planet Cute:Character Boy", 400, 500)
    fontSize(25)
    text("LEVEL " .. level, WIDTH/2, HEIGHT - 50)
end

function touched(touch)
    if touch.state==MOVING then
        if touch.x < 425 and touch.x > 375 and touch.y < 525 and touch.y > 475 then sound(DATA, "ZgBAPQBGPhleSiRr8Jp1vjxX4j6CwGU+TABhWw9EOFlAO2E0")
        if touch.state==ENDED then
        sound("A Hero's Quest:Hurt 1")
        sound("A Hero's Quest:Hurt 2")
        sound("A Hero's Quest:Hurt 3")
        sound("A Hero's Quest:Hurt 5")
        sound("A Hero's Quest:Level Up")
        print("LEVEL UP!")
        level = level + 1
        speech.say("New Level! Thanks for playing the game!")
        end
        end
    end
end

How is the touch.state==ENDED code going to be reached when it is within the scope of a touch.state==MOVING statement?

What is the correct way to build a string from mixed data before calling text? Should you use implicit conversions for a string assignment or should you build it on the fly calling string.format() as necessary?

Personally, I would be embarrassed to have labeled the program above as “better”. Tokout’s post presented a problem I hadn’t thought about (string conversion in function calls) which showed up as soon as I pasted the code and a small debugging puzzle, when the level didn’t change. I believe corrections should be tested, before being labeled as “better”.

I appreciate the chance to ask about building the string on the fly. Thank you Tokout.