Hi, I’m using the director class for my game, and I need some assistance in using the Director Class for using the swiping up and down, and left and right function together on the same object for changing screens. It seems that DC can’t detect the difference between up and down, and left and right when they are on the same object. Is there a any solution to this? Help is appreciated. Thanks. [import]uid: 66329 topic_id: 17351 reply_id: 317351[/import]
What you could do is first use the code located here:
http://developer.anscamobile.com/code/object-drag-direction
Then have a function that says
[lua] local function changeScene ()
if e.phase == ended then
if string_dir=“Up” then
director:changeScene(“desiredScene”, “desiredTransition”)
elseif string_dir=“Down” then
director:changeScene(“desiredScene”, “desiredTransition”)
elseif string_dir=“Left” then
director:changeScene(“desiredScene”, “desiredTransition”)
elseif string_dir=“Right” then
director:changeScene(“desiredScene”, “desiredTransition”)
end
end
I don’t know if it will work, but its looks like it should [import]uid: 66117 topic_id: 17351 reply_id: 65699[/import]
Thanks Joe! This is exactly what I was looking for. The only problem is that function that you provided; it causes an error in the terminal. Nevertheless, I can just put the director:changeScene function straight into the object-drag-direction code. Thanks again! [import]uid: 66329 topic_id: 17351 reply_id: 65832[/import]
No prob! What does the terminal say? [import]uid: 66117 topic_id: 17351 reply_id: 65833[/import]
Well, it would appear that I spoke too soon. When I put the changeScene function in the other code like I said before, it causes Corona to crash :p.
When I put in the code that you suggested, I get the following error:
[code]Runtime error
/Users/myUser/Documents/test/director.lua:934: ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’
stack traceback:
[C]: ?
[C]: in function ‘insert’
/Users/myUser/Documents/test/director.lua:934: in function ‘_listener’
?: in function <?:501>
?: in function <?:215>
[import]uid: 66329 topic_id: 17351 reply_id: 65834[/import]
Okay… let me create a project real quick and try this out. [import]uid: 66117 topic_id: 17351 reply_id: 65835[/import]
Are you using the new 15 line director or the old one? [import]uid: 66117 topic_id: 17351 reply_id: 65839[/import]
I’m using the old one, I’ve heard of the 15 line one, but haven’t tried it out yet.
[import]uid: 66329 topic_id: 17351 reply_id: 65842[/import]
If you download the 1.4 version here:
http://developer.anscamobile.com/code/director-class-10
He has a way to change scenes by swiping horizontally. Maybe it could be implemented to vertical swiping to… [import]uid: 66117 topic_id: 17351 reply_id: 65843[/import]
Okay, grab your popcorn. I have kinda created my own way of detecting which way you swipe and then changing the scene. Here is main.lua:
[lua]director = require(‘director’)
local function main()
director:changeScene(‘menu’)
end
main()[/lua]
and here is the menu.lua:
[lua]local _M = {}
local prevx = 0
local prevy = 0
local string_dir = nil
function _M.new( )
local localGroup = display.newGroup()
local rectangle = display.newRect(0,100,display.contentWidth,400)
rectangle:setFillColor(255,0,0)
localGroup:insert(rectangle)
local placeMarker = display.newCircle(0,0,15)
placeMarker:setFillColor(0,0,255)
localGroup:insert(placeMarker)
local directionTXT = display.newText("direction: ",50,50,nil,25)
localGroup:insert(directionTXT
local function changeTheScene(event)
if event.phase == “began” then
placeMarker:toFront()
placeMarker.x = event.x
placeMarker.y = event.y
elseif event.phase == “ended” then
placeMarker:toFront()
print “finger moved”
print (placeMarker.x)
print (event.x)
if placeMarker.x > event.x then
directionTXT.text = “you slid left”
director:changeScene(“Left”)
elseif placeMarker.x < event.x then
directionTXT.text = “you slid to the right”
end
end
end
rectangle:addEventListener(“touch”, changeTheScene)
return localGroup
end
return _M[/lua]
In the game the place marker would be transparent.
Everything works fine… as long as you don’t tap on the screen when it changes to Left.lua. It crashes if you do that. [import]uid: 66117 topic_id: 17351 reply_id: 65849[/import]
Ok, This is great, but it doesn’t quite have all of the functionality that I’m looking for. It’s rather important that I can be able to use it vertically too. That’s why I got excited over seeing the Object Drag Direction code. I’m going to work with the code that you demonstrated, and try to modify the Object Drag Direction code to see if I can get the effect that I’m looking for. Thank you very much for all of your help so far
[import]uid: 66329 topic_id: 17351 reply_id: 65860[/import]
Okay, I think I got what you want. I haven’t made all the scenes. Just the “left” one:
[lua]–to store previous position of x and y
local _M = {}
local prevx = 0
local prevy = 0
local direction = “right”
function _M.new( )
local localGroup = display.newGroup()
local rectangle = display.newRect(0,100,display.contentWidth,400)
rectangle:setFillColor(255,0,0)
localGroup:insert(rectangle)
local placeMarker = display.newCircle(0,0,15)
placeMarker:setFillColor(0,0,255)
localGroup:insert(placeMarker)
local directionTXT = display.newText("direction: ",50,50,nil,25)
localGroup:insert(directionTXT)
–local director = require (“director”)
local function onTouch( event )
local t = event.target
local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
prevx = event.x
prevy = event.y
elseif t.isFocus then
if “moved” == phase then
– t.x = event.x - t.x0
– t.y = event.y - t.y0
local dx = prevx - event.x
local dy = prevy - event.y
local distance = dx * dx + dy * dy
if distance>400 then
local angle = math.atan2(dy,dx) * 57.2957795
local string_dir
if angle>=22*-1 and angle<23 then
string_dir=“Left”
direction = “left”
elseif angle>=23 and angle<68 then
string_dir=“Up Left”
direction =“Up Left”
elseif angle>=68 and angle<113 then
string_dir=“Up”
direction = “Up”
elseif angle>=113 and angle<158 then
string_dir=“Up Right”
direction = “Up Right”
elseif angle>=135 or angle<157*-1 then
string_dir=“Right”
direction = “Right”
elseif angle>=157*-1 and angle<112*-1 then
string_dir=“Down Right”
direction =“Down Right”
elseif angle>=112*-1 and angle<67*-1 then
string_dir=“Down”
direction = “Down”
elseif angle>=67*-1 and angle<22*-1 then
string_dir=“Down Left”
direction = “Down Left”
end
prevx=event.x
prevy=event.y
directionTXT.text = "Direction : "…string_dir
end
– if phase == “ended” then
– director:changeScene(“Left”)
– end
end
end
if string_dir == “Left” then
print “verified”
end
return true
end
rectangle:addEventListener(“touch”, onTouch)
local function switch (e)
if e.phase == “ended” then
if direction == “left” then
print (direction)
director:changeScene(‘left’)
elseif direction == “Up Left” then
print (direction)
director:changeScene(“Up Left”)
elseif direction == “Up” then
print (direction)
director:changeScene(“Up” )
elseif direction == “Up Right” then
print (direction)
director:changeScene(“Up Right”)
elseif direction == “Right” then
print (direction)
director:changeScene(“Right”)
elseif direction == “Down Right” then
print (direction)
director:changeScene(“Down Right” )
elseif direction == “Down” then
print (direction)
director:changeScene(“Down”)
elseif direction == “Down Left” then
print (direction)
director:changeScene( “Down Left”)
end
end
end
rectangle:addEventListener(“touch”, switch)
return localGroup
end
return _M[/lua]
Once again, it works, as long as you don’t click on the screen in the left.lua scene. [import]uid: 66117 topic_id: 17351 reply_id: 65861[/import]
Ok, this is actually almost exactly what I have been searching for! I see what you mean about the crashes. It happens on all other scenes also. I also notice that it doesn’t crash when the left.lua has the code shown below taken out:
[code]
–left.lua
module(…, package.seeall)
function new()
local localGroup = display.newGroup()
return localGroup
end
[import]uid: 66329 topic_id: 17351 reply_id: 66315[/import]