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.
Label: corona android iphone ipad building error inmobi problem display screen View image tableView failed object physics touch button game lua config event listener |
Director Class swipe for a game help
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 |
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!
No prob! What does the terminal say?
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:
1 2 3 4 5 6 7 8 | 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> |
Okay... let me create a project real quick and try this out.
Are you using the new 15 line director or the old one?
I'm using the old one, I've heard of the 15 line one, but haven't tried it out yet.
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...
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:
1 2 3 4 5 | director = require('director') local function main() director:changeScene('menu') end main() |
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 :)
Okay, I think I got what you want. I haven't made all the scenes. Just the "left" one:
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | --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 |
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
Question about Developer
EASILY Make Your Text Sharp on Retina Displays
Attempt to index field nil value
Preview: SPIN UP, A Pseudo-3D Action Game
AppAdvice is doing a Tilt-a-Bowl giveaway!
Hamster Vice Comic Book now in App Store
Video in android
use of shape in physics.addbody( )
The Unofficial Corona Textbook