Movement Buttons?

I want my game to have two buttons on the screen. I want to have an arrow facing left, and an arrow facing right. I know how to display them, but not tell them to move my game character to the left or right. Does anyone have any code they're willing to share?

You have a few options

1) increment the x ie.. player.x = player.x + 1
2) use transitions
3) if your using physics you could use setLinearVelocity

Hope that helps :)

I'm going to go with option 1. What would the code look like?

Also, I want my sprite to turn into it's animating.gif form when it's walking. As soon as it stops, I want it to turn back into it's standing form. Can I use two images for this and mirror them for the other side, or do I need four images?

It would look like this (and yeah you can use two and mirror)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
local player = display.newImage("player.png")
 
--Table to hold our move buttons
local buttons = {}
 
--Table to hold the properties for the move buttons
local buttonProperties = {
    {img = "walkButtonRight.png", x = 100, y = 100, myWalkType = "Right"},
    {img = "walkButtonLeft.png", x = 200, y = 100, myWalkType = "Left"}
}
 
--Function to make the player move/walk
local function walk(event)
    local phase = event.phase
    local target = event.target
 
    if phase == "began" then
        if target.myWalkType == "Right" then
            player.xScale = 1 --Flip player image ( I assume its facing right to begin with)
            player.x = player.x + 1
        elseif target.myWalkType == "Left" then
            player.xScale = -1 --Flip player image ( I assume its facing right to begin with)
            player.x = player.x - 1
        end
    end
 
    return true
end
 
 
--Load up buttons, set properties and add listeners
for i = 1, #buttonProperties do
    buttons[i] = display.newImage(buttonProperties[i].img)
    buttons[i].x = buttonProperties[i].x
    buttons[i].y = buttonProperties[i].y
    buttons[i].myWalkType = buttonProperties[i].myWalkType
 
    buttons[i]:addEventListener("touch", walk)
end

I believe it's better practice to use

player:translate(-1,0)

instead of

player.x = player.x - 1

as the former doesn't need to read the value first. It may not make a noticeable difference in performance, but it's a good habit to get into.

also it looks like line 22 should actually read

player.xScale = -1 --Flip player image ( I assume its facing right to begin with)

(instead of yScale)

You were correct about line 22, i modified my post. Thanks for pointing that out

Would like to thank you Danny.

Your sample code surely help me out learning about character`s movement as well.

Regards,
Rodrigo.

I want the main character to transform into it's animating.gif when it's moving, and back to it's stationary when it's stopped. What do you guys suggest?

This is what I currently have.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function createButtons()
        local buttons = {}
        local buttonProperties =
        {
                {img = "visuals/game_buttonRight.png", x = 200, y = 100, direction = "Right"},
                {img = "visuals/game_buttonLeft.png", x = 100, y = 100, direction = "Left"},
        }
        local function walk(event)
                local phase = event.phase
                local target = event.target
                if phase == "began" then
                        if target.direction == "Right" then
                                pig.xScale = 1
                                pig.x = pig.x + 1
                        elseif target.direction == "Left" then
                                pig.xScale = -1
                                pig.x = pig.x - 1
                        end
                end
                return true
        end
        for i = 1, #buttonProperties do
                buttons[i] = display.newImage(buttonProperties[i].img)
                buttons[i].x = buttonProperties[i].x
                buttons[i].y = buttonProperties[i].y
                buttons[i].directon = buttonProperties[i].direction
                buttons[i]:addEventListener("touch", walk)
        end
end

Also, I think my buttons' placement point (for lack of a better word) is centered, which makes things problematic when placing them on the screen. All of my other images have placement points at the top left corner of their image region, but not the buttons. Is this something that has to do with the code? Or could it be because it's not in the gameGroup?

Hmm... also, my buttons are responding. I put a print statement when they were pressed, but nothing happens.

IKinx - we don't support animated GIFs, we use spritesheets.

To give you an idea of moving a character with directional buttons AND playing an animation (that stops when the character does) please take a look at this tutorial; http://techority.com/2011/05/01/using-sprites-in-your-corona-iphone-application/

That should help get you started.

Peach :)

I screwed up somewhere. :P

Error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Windows simulator build date: Jul 27 2011 @ 06:45:53
 
Copyright (C) 2009-2011  A n s c a ,  I n c .
        Version: 2.0.0
        Build: 2011.591
WARNING: Failed to find image(game_pig.png)
Runtime error
        ...rs\Kyle\Documents\Coding\Corona\Makin' Bank\main.lua:26: bad argument
 #1 to 'newSpriteSet' (sprite.SpriteSheet expected, got nil)
stack traceback:
        [C]: ?
        [C]: in function 'newSpriteSet'
        ...rs\Kyle\Documents\Coding\Corona\Makin' Bank\mRuntime error: ...rs\Kyl
e\Documents\Coding\Corona\Makin' Bank\main.lua:26: bad argument #1 to 'newSprite
Set' (sprite.SpriteSheet expected, got nil)
stack traceback:
        [C]: ?
        [C]: in function 'newSpriteSet'
        ...rs\Kyle\Documents\Coding\Corona\Makin' Bank\main.lua:26: in
views:1748 update:2011/11/17 9:28:17
corona forums © 2003-2011