diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fbabf5b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Export/ diff --git a/MultiTouchExample.hxproj b/MultiTouchExample.hxproj old mode 100755 new mode 100644 index 896b11d..75330ca --- a/MultiTouchExample.hxproj +++ b/MultiTouchExample.hxproj @@ -1,56 +1,57 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..009b037 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# OpenFL-Multitouch-Example +A quick example of how to handle multitouch input using Haxe3 and Openfl. + +# Description +Shows a circle where the user touch and keep following finger movements until release. +It (obviously) handle multi-touch. +It doesn't work with mouse (on desktop targets), but it works in the (e|si)mulator. diff --git a/application.build b/application.build deleted file mode 100755 index ca7bf83..0000000 --- a/application.build +++ /dev/null @@ -1 +0,0 @@ -13 \ No newline at end of file diff --git a/application.nmml b/application.nmml deleted file mode 100755 index ab09872..0000000 --- a/application.nmml +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/assets/nme.svg b/assets/nme.svg deleted file mode 100755 index cd02515..0000000 --- a/assets/nme.svg +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/assets/openfl.svg b/assets/openfl.svg new file mode 100644 index 0000000..ff69474 --- /dev/null +++ b/assets/openfl.svg @@ -0,0 +1,114 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + diff --git a/project.xml b/project.xml new file mode 100644 index 0000000..519860a --- /dev/null +++ b/project.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/Main.hx b/src/Main.hx old mode 100755 new mode 100644 index ac5316e..6500f12 --- a/src/Main.hx +++ b/src/Main.hx @@ -1,114 +1,114 @@ -package ; - -import nme.Lib; -import nme.display.FPS; -import nme.display.Sprite; -import nme.events.Event; -import nme.events.TouchEvent; -import nme.ui.Multitouch; -import nme.ui.MultitouchInputMode; - -/** - * Simple multitouch example - tested with iOS - * @author James Frost (@frosty - http://www.jamesfrost.co.uk) - */ - -class Main extends Sprite -{ - // Keep track of whether multitouch is supported on this device - private var multiTouchSupported : Bool; - // Store our - private var touches : IntHash; - - public function new() - { - super(); - #if iphone - Lib.current.stage.addEventListener(Event.RESIZE, init); - #else - addEventListener(Event.ADDED_TO_STAGE, init); - #end - } - - private function init(e) - { - // Add an FPS counter - var fps : FPS = new FPS(); - addChild(fps); - - // Declare our touches hash - touches = new IntHash(); - - // Find out whether multitouch is supported - multiTouchSupported = Multitouch.supportsTouchEvents; - if (multiTouchSupported) - { - // If so, set the input mode and hook up our event handlers - // TOUCH_POINT means simple touch events will be dispatched, - // rather than gestures or mouse events - Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; - - Lib.current.stage.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin); - Lib.current.stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove); - Lib.current.stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd); - } - - trace("Using multitouch: " + multiTouchSupported); - } - - private function onTouchBegin(e:TouchEvent):Void - { - trace("onTouchBegin"); - - // Create a new sprite with a random colour - var touchSprite = new Sprite(); - var fill = (Std.int (Math.random () * 0xFFFFFF) ); - touchSprite.graphics.beginFill(fill); - touchSprite.graphics.drawCircle(0, 0, Lib.current.stage.dpiScale * 50); - touchSprite.graphics.endFill(); - - // Move the sprite to the same position as the touch - touchSprite.x = e.stageX; - touchSprite.y = e.stageY; - - addChild(touchSprite); - - // Put the sprite into our touches array, referenced by touch ID - touches.set(e.touchPointID, touchSprite); - } - - private function onTouchMove(e:TouchEvent):Void - { - trace("onTouchMove"); - - // Find the matching sprite in our touches array - var touchSprite : Sprite = touches.get(e.touchPointID); - - // Update its position - touchSprite.x = e.stageX; - touchSprite.y = e.stageY; - } - - private function onTouchEnd(e:TouchEvent):Void - { - trace("onTouchEnd"); - - // Find the matching sprite in our touches array - var touchSprite : Sprite = touches.get(e.touchPointID); - - // Remove the sprite from the stage and the array - removeChild(touchSprite); - touches.remove(e.touchPointID); - } - - static public function main() - { - var stage = Lib.current.stage; - stage.scaleMode = nme.display.StageScaleMode.NO_SCALE; - stage.align = nme.display.StageAlign.TOP_LEFT; - - Lib.current.addChild(new Main()); - } - - -} +package ; + +import openfl.Lib; +import openfl.display.FPS; +import openfl.display.Sprite; +import openfl.events.Event; +import openfl.events.TouchEvent; +import openfl.ui.Multitouch; +import openfl.ui.MultitouchInputMode; + +/** + * Simple multitouch example - tested with iOS + * @author James Frost (@frosty - http://www.jamesfrost.co.uk) + */ + +class Main extends Sprite +{ + // Keep track of whether multitouch is supported on this device + private var multiTouchSupported : Bool; + // Store our + private var touches : Map; + + public function new() + { + super(); + #if iphone + Lib.current.stage.addEventListener(Event.RESIZE, init); + #else + addEventListener(Event.ADDED_TO_STAGE, init); + #end + } + + private function init(e) + { + // Add an FPS counter + var fps : FPS = new FPS(); + addChild(fps); + + // Declare our touches hash + touches = new Map(); + + // Find out whether multitouch is supported + multiTouchSupported = Multitouch.supportsTouchEvents; + if (multiTouchSupported) + { + // If so, set the input mode and hook up our event handlers + // TOUCH_POINT means simple touch events will be dispatched, + // rather than gestures or mouse events + Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; + + Lib.current.stage.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin); + Lib.current.stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove); + Lib.current.stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd); + } + + trace("Using multitouch: " + multiTouchSupported); + } + + private function onTouchBegin(e:TouchEvent):Void + { + trace("onTouchBegin"); + + // Create a new sprite with a random colour + var touchSprite = new Sprite(); + var fill = (Std.int (Math.random () * 0xFFFFFF) ); + touchSprite.graphics.beginFill(fill); + touchSprite.graphics.drawCircle(0, 0, Lib.current.stage.dpiScale * 50); + touchSprite.graphics.endFill(); + + // Move the sprite to the same position as the touch + touchSprite.x = e.stageX; + touchSprite.y = e.stageY; + + addChild(touchSprite); + + // Put the sprite into our touches array, referenced by touch ID + touches.set(e.touchPointID, touchSprite); + } + + private function onTouchMove(e:TouchEvent):Void + { + trace("onTouchMove"); + + // Find the matching sprite in our touches array + var touchSprite : Sprite = touches.get(e.touchPointID); + + // Update its position + touchSprite.x = e.stageX; + touchSprite.y = e.stageY; + } + + private function onTouchEnd(e:TouchEvent):Void + { + trace("onTouchEnd"); + + // Find the matching sprite in our touches array + var touchSprite : Sprite = touches.get(e.touchPointID); + + // Remove the sprite from the stage and the array + removeChild(touchSprite); + touches.remove(e.touchPointID); + } + + static public function main() + { + var stage = Lib.current.stage; + stage.scaleMode = openfl.display.StageScaleMode.NO_SCALE; + stage.align = openfl.display.StageAlign.TOP_LEFT; + + Lib.current.addChild(new Main()); + } + + +} \ No newline at end of file