Help with touch event

Hello everybody,

I new in Corona and I´m trying my first app.
I have a object that move by 4 buttons (up/donw/left/right) with applyforce.

While the button are press I want apply force in the object, so, for that Im using this code for each button:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function btUp( event )
    if event.phase == "began" then
                impUp = true
    elseif event.phase == "ended" or event.phase == "cancelled" or event.phase == "moved" then
                impUp = false
                nave:stopAtFrame(1)     
    end
end
 
function impulseUp()    
        if (impUp and combustivel > 0) then
                nave:applyForce( 0, -0.5, nave.x, nave.y )
                nave:stopAtFrame(3)     
                combustivel = combustivel - 2
                health = combustivel
                moveHealth()
        end
end
upimg:addEventListener("touch", btUp)
Runtime:addEventListener("enterFrame", impulseUp)

In second code, the correct line 13 is

1
elseif event.phase == "ended" or event.phase == "cancelled" or event.phase == "moved" then

Why not remove the check for "moved"...that may fix the issue.

Also, for the 2nd code example...I think you want to check for event.phase...not just the "phase" variable. I don't see variable defined.

Hello indiegamepod,

Thx for your reply.

You right about the second code, I did this correction in first comment.

The "moved" event ocurr when you move a finger over a object with "touch" listener?
If this is correct, in those algoritm when "moved" occur, was like I took off the finger from the button, but my problem is that the button answer like I was pressing all time.

But I´m new in Corona and can be talking smething nonsense. I try without "moved"

I remove "moved" but nothing..

I forgot something essencial... This only happen in Iphone Device... In simulator everything work fine. i dont try in Android

Can I see the "prints" for debug when run in iphone device?

Have you seen this tutorial by Peach Pellen?

http://techority.com/2011/02/14/controlling-a-character-with-a-d-pad/

Hello elbowroomapps,

Very nice tutorial, I think this approach will resolve my problem.

1
2
3
4
5
6
7
8
local function stop (event) 
   if event.phase =="ended" then
      motionx = 0
      motiony = 0
   end
end
 
Runtime:addEventListener("touch", stop )

I'm glad it helped.

The thanks should really go to Peach Pellen.

I love it when I come into a thread, ready to help - and see that the problem has already been solved.

Admittedly, I love it a little more when it's been solved using one of my tutorials ;)

Thanks guys!

Hello Peach,

Sry but the problem wasnt resolve.. :(

I will clear all code and leave only the problem part, test in iPhone, and, if persist, put here.

This is a Multitouch app, so, in simulator run fine (I cannt press 2 button at same time, course). But, is possible I see a terminal for debug when run in iPhone? Or log file?

Thx for help.

yeah... I cleaned all code and put only the essential to reproduce the problem. The problem doesn´t happen all time, but happens.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
system.activate( "multitouch" )
local physics = require( "physics" )
physics.start()
physics.setGravity(0, 2.5) 
 
local esq
local dir
local freio
local nave
 
------  PREPARACAO DO AMBIENTE
function setUpBotoes()
        freio = display.newImage( "botaoredondo.png");  
        freio.x = 45
        freio.y = 295
        esq = display.newImage( "setaesq.png"); 
        esq.x = 375
        esq.y = 295
        dir = display.newImage( "setadir.png"); 
        dir.x = 450
        dir.y = 295
end
 
function setUpGround()
        local ground = display.newImage( "ground.png" )
        ground.x = display.contentWidth / 2
        ground.y = 270  
        physics.addBody( ground, "static", { friction=1, bounce=0, filter = {categoryBits = 4, maskBits = 2} })
end
 
 
 
------------ JOGABILIDADE
function btFreio( event )
    if event.phase == "began" then
                function impulsoFreio() 
                        nave:applyForce( 0, -0.5, nave.x, nave.y )
                end
                Runtime:addEventListener("enterFrame", impulsoFreio)
    else
                Runtime:removeEventListener("enterFrame", impulsoFreio)
    end
end
 
 
 
function btDir( event )
        if event.phase == "began" then
                function impulsoDir()
                        nave:applyForce( -0.15, 0, nave.x, nave.y )
                end     
                Runtime:addEventListener("enterFrame", impulsoDir)
    else
                Runtime:removeEventListener("enterFrame", impulsoDir)
    end
end
 
 
function btEsq( event )
    if event.phase == "began" then
                function impulsoEsq()
                        nave:applyForce( 0.15, 0, nave.x, nave.y )
                end
                Runtime:addEventListener("enterFrame", impulsoEsq)
    else
                Runtime:removeEventListener("enterFrame", impulsoEsq)
    end
end
 
function setUpNave()
        nave = display.newImage("nave25.png")
        nave.x = 200
        nave.y = 30
        nave.linearDamping = 5
        naveshape01 = {-5,5, -5,-6, -1,-12, 0,-16, 6,-7, 6,5, 6,11, 4,14, -3,14, -5,10 }
        naveshape02 = {-12,8, -10,4, -6,5, -6,10, -8,16, -11,16, -12,11}
        naveshape03 = {7,6, 10,4, 12,8, 12,15, 10,16, 8,15, 7,11}
        
        physics.addBody(nave, "dynamic", 
        { density = 0.1, friction=0.01, shape = naveshape01, filter = {categoryBits = 2, maskBits = 12}},
        { density = 0.1, friction=0.01, shape = naveshape02, filter = {categoryBits = 2, maskBits = 12}},
        { density = 0.1, friction=0.01, shape = naveshape03, filter = {categoryBits = 2, maskBits = 12}}        
        )       
        nave.isFixedRotation = true
end
 
 
function main()
        setUpNave()
        setUpBotoes()
        setUpGround()
        esq:addEventListener("touch", btEsq)
        dir:addEventListener("touch", btDir)
        freio:addEventListener("touch", btFreio)
end
 
main()

If you want to see the log files on the iPhone, I use Xcode. Open the organizer on Xcode and connect your iPhone to it. Then drag the file of your game to the applications folder. Run and teat you game on the iPhone and the console and log files are present on Xcode!

I don't know if I explained it good, hope I helped...
And I also believe there are many ways but that is just the way I do it.

Hello brian,

This is like I do to test my app in iPhone, but I tried before and nothing (no one print("something")) went to log or console in xcode (my Iphone still puggled).

Need some diretive or command in main.lua to send print() to console or log in xcode?

After many test, many prints, many differents codes to do the same thing...

my conclusion is: sometimes (1 in 20) the ended/moved/canceled phase in touch event isn't detect. :( Maybe the time between the press and the lift finger. Maybe some problem with applyforce use...

If somebody want check this, pls ask me about the procedure.

thx Claudio

For me at least, it seems like a group of code comes every min or so.

For one of my last games i had a perform with delay timer and every second i printed something. It wouldn't come in every second, but a group would come in every min or so( maybe a little longer).

and just try

print("something")

And also since it is not working you should look at the code made by peach pellen(link is at the start of the forum). Maybe there is something you are doing wrong, and most likely your code has similar characteristics.

Hey again, sorry to hear that.

May I ask, is there a reason you are using apply force (which stacks) rather than applying linear velocity?

I am sure we can figure out a solution, I'm just wondering if it might be an option to use that instead - if so that could potentially be the way to go.

Let us know :)

Peach

Hey Peach,

It is a spacecraft in a low gravity environment, then the "applyforce" represented well the impulse jet stabilizers. This weekend I will try to use the linear velocity, but for a good representation, while the button is pressed, the speed increase and should stop when the finger is removed, then I fear that the same problem occurs.

I understand.

Can you try the print statements that Brian suggested, to help troubleshoot? :)

Hi Peach,

Note that their algorithm uses a constant speed in an environment monotouch and no gravity. I will adapt it to applyforce and multitouch. I need the multitouch because the user has to act against gravity almost all the time, even when going left or right. I will put here when functional.

Hey Peach!

I was adapting your code, but I had problems early on with the routine below:

1
2
3
4
5
6
7
local function stop (event)
if event.phase =="ended" then
motionx = 0
motiony = 0
end
end
Runtime:addEventListener("touch", stop )

Very pleased to hear that; will give your code a test myself later on, see it in action! :)

views:1877 update:2011/10/11 8:57:46
corona forums © 2003-2011