2010년 10월 21일 목요일

flash touch 샘플코드

Hi. Could someone explain how come one both of these examples work when compiled to iPhone, but only the Example2 works with MacBook Pro Touchpad? MTouch works with gestures, but not with the TouchEvent.

Note that the Example1 has a maximum touches left, but when I do the same thing for the Example2 I get "zero" as the result, but again... it still works on the touchpad in MacBook.

Seems to me that it's the Multitouch.inputMode = MultitouchInputMode.GESTURE and TransformGestureEvent. Note that in order to move the image in the Example2 example you need to use both fingers - this is kind of crappy. I wanted to add a regular TouchEvent to handle simple moving or even tracing something to the output box (look at the code), but it simply doesn't work.

Also... it would be great if someone could show me the way of testing this with a mouse - basically can I somehow code for taps and touch events, but test with my mouse (mouseevent) without rewriting or commenting out a lot of code?

Thanks

Example1

import flash.display.Sprite;
import flash.events.TouchEvent;
import flash.text.AntiAliasType;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;


var dots:Object;
var labels:Object;
var labelFormat:TextFormat;
var dotCount:uint;
var dotsLeft:TextField;
var LABEL_SPACING:uint = 15;


this.labelFormat = new TextFormat();
labelFormat.color = 0xACF0F2;
labelFormat.font = "Helvetica";
labelFormat.size = 11;

this.dotCount = 0;

this.dotsLeft = new TextField();
this.dotsLeft.width = 300;
this.dotsLeft.defaultTextFormat = this.labelFormat;
this.dotsLeft.x = 3;
this.dotsLeft.y = 0;
this.stage.addChild(this.dotsLeft);
this.updateDotsLeft();

this.dots = new Object();
this.labels = new Object();

Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
this.stage.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
this.stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
this.stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);


function onTouchBegin(e:TouchEvent):void
{
if (this.dotCount == Multitouch.maxTouchPoints)
{
return;
}
var dot:Sprite = this.getCircle();
dot.x = e.stageX;
dot.y = e.stageY;
this.stage.addChild(dot);
dot.startTouchDrag(e.touchPointID, true);
this.dots[e.touchPointID] = dot;

++this.dotCount;

var label:TextField = this.getLabel(e.stageX + ", " + e.stageY);
label.x = 3;
label.y = this.dotCount * LABEL_SPACING;
this.stage.addChild(label);
this.labels[e.touchPointID] = label;

this.updateDotsLeft();
}

function onTouchMove(e:TouchEvent):void
{
var label:TextField = this.labels[e.touchPointID];
label.text = (e.stageX + ", " + e.stageY);
}

function onTouchEnd(e:TouchEvent):void
{
var dot:Sprite = this.dots[e.touchPointID];
var label:TextField = this.labels[e.touchPointID];

this.stage.removeChild(dot);
this.stage.removeChild(label);

delete this.dots[e.touchPointID];
delete this.labels[e.touchPointID];

--this.dotCount;

this.updateDotsLeft();
}

function getCircle(circumference:uint = 40):Sprite
{
var circle:Sprite = new Sprite();
circle.graphics.beginFill(0x1695A3);
circle.graphics.drawCircle(0, 0, circumference);
return circle;
}

function getLabel(initialText:String):TextField
{
var label:TextField = new TextField();
label.defaultTextFormat = this.labelFormat;
label.selectable = false;
label.antiAliasType = AntiAliasType.ADVANCED;
label.text = initialText;
return label;
}

function updateDotsLeft():void
{
this.dotsLeft.text = "Touches Remaining: " + (Multitouch.maxTouchPoints - this.dotCount);
}


Example2


import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.events.TransformGestureEvent;
import flash.events.TouchEvent;
import flash.text.TextField;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;


Multitouch.inputMode = MultitouchInputMode.GESTURE;
var sq:Sprite;


sq = new Sprite();
addChild(sq);


this.addEventListener(TransformGestureEvent.GESTURE_ZOOM, scaleObj);
this.addEventListener(TransformGestureEvent.GESTURE_PAN, panObj);
this.addEventListener(TransformGestureEvent.GESTURE_ROTATE, rotObj);
this.addEventListener(TouchEvent.TOUCH_TAP, taptap);

var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, addImg);
l.load(new URLRequest("http://lobby.blox.pl/resource/LOBBY_logo1.jpg"));

function disp():void{
dispatchEvent(new Event(TouchEvent.TOUCH_TAP));
}

function taptap(e:TouchEvent):void{
trace("this doesn't work for some reason");
}

function addImg(e:Event):void{
sq.addChild(e.target.content);
sq.scaleY = sq.scaleX = .2;
}


function rotObj(e:TransformGestureEvent):void{
sq.rotation += e.rotation;
}


function panObj(e:TransformGestureEvent):void{
sq.x += e.offsetX * 2;
sq.y += e.offsetY * 2;
}


function scaleObj(e:TransformGestureEvent):void{
sq.scaleX *= e.scaleX;
sq.scaleY *= e.scaleY;
}

trace(Multitouch.maxTouchPoints);

댓글 없음:

댓글 쓰기