Gestouch is not working - actionscript-3

I use starling for book reader app. When i change the book, i'm disposing the current starling and create new starling instance.But when I created new starling, Gestouch doesn't works.
How to restart gestouch for a new starling instance
gestouch code:
gallery.addEventListener(MouseEvent.CLICK,book);
onKeyDown(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.BACK )
{
event.preventDefault();
event.stopImmediatePropagation();
this.starling.stop();
this.starling.dispose();
this.starling = null;
}
}
book(e:MouseEvent):void
{
this.starling = new Starling(RacTestApp,stage,viewPort);
Gestouch.inputAdapter ||= new NativeInputAdapter(stage);
Gestouch.addDisplayListAdapter(starling.display.DisplayObject, new StarlingDisplayListAdapter());
Gestouch.addTouchHitTester(new StarlingTouchHitTester(this.starling), -1);
this.starling.start();
}
thank you

Hey Guys I got the answer:
OnKeydown(){
Gestouch.removeTouchHitTester(this.myStarlingTouchHitTester);
}
book(){
this.myStarlingTouchHitTester = new StarlingTouchHitTester(this.starling);
Gestouch.addTouchHitTester(this.myStarlingTouchHitTester, -1);
}
We have to add and remove TouchHitTester, if we are using multiple starling instances.

Related

unable to remove movieclip itself from stage as3

I had a button, with that button a movieclip is called on the stage and it is working fine with the below script.
I just updated the script and it is now working fine with the removing of movieclip from the stage, but now the issue came with playing the movieclip. Now the movieclip is not playing well.
var btn: btn_Lemon = new btn_Lemon();
var mc: mc_lemon = new mc_lemon();
addChild(btn);
btn.x = 304.45;
btn.y = 209.8;
btn.addEventListener(MouseEvent.MOUSE_DOWN, login, false, 0, true);
function login(event: MouseEvent): void {
stage.addChild(mc);
mc.x = 304.45;
mc.y = 209.8;
mc.addEventListener(Event.ENTER_FRAME, fadeOver, false, 0, true);
}
function fadeOver(event: Event): void {
if (event.currentTarget.currentFrame == 25) {
event.currentTarget.removeEventListener(Event.ENTER_FRAME, fadeOver);
stage.removeChild(mc);
}
}
What is going on please help somebody
Presuming it's a timeline you're trying to play inside lemon_mc and it's the play method that doesn't seem to run properly.
In my experience Flash can sometimes be a bit fickle with playing as I recall.
Here are some pointers for pushing the MC to play:
Force the a mc to play at the end of the
function login(event: MouseEvent): void {
stage.addChild(mc);
mc.x = 304.45;
mc.y = 209.8;
mc.addEventListener(Event.ENTER_FRAME, fadeOver, false, 0, true);
mc.play();
}
Simple add
play();
at the first frame inside the movie clip you want to play.
Also if the playback is a bit fickle at certain scenarios you can define with code, then you can use a test:
if (mc.isPlaying == false) {
mc.play();
}
As a beginner I found the as3 documentation from adobe to be easy to understand and apply. You might find some other properties to read or methods you can manipulate to force the play method:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/MovieClip.html

AS3 hitTestObject not working at all

I've been working on a large project in Flash CS6 and ActionScript 3 which includes jigsaws etc. I have been using hitTestObject throughout the project and it had been working fine, then all of a sudden it stopped working. It won't work in any new instances or even where it worked before. I created a small test piece, and this isn't working either:
import flash.events.MouseEvent;
hit_txt.visible = false;
object2_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
function drag(e:MouseEvent = null):void {
object2_mc.startDrag();
}
if (object2_mc.hitTestObject(object1_mc)) {
hit_txt.visible = true;
}
Any help or suggestions would be greatly appreciated, thanks in advance.
You are not stating when to do the checking of the hit test. So either you check by enter frame, or while you move the object
object2_mc.addEventListener(MouseEvent.MOUSE_MOVE, move);
function move(e:MouseEvent = null):void
{
if (object2_mc.hitTestObject(object1_mc))
{
hit_txt.visible = true;
}
}

Problems with an object appearing on a frame I don't want it to appear on AS3

I am a total noob at AS3, roughly 1 year experience so please be lenient with me :)
I currently am making an endless runner game and I'm making the obstacles spawn using this method
var therespawn:RespawnObject;
var thecone:trafficcone;
var started:Boolean = false;
var dx:Number = 10;
var dy:Number = 10;
stage.addEventListener(Event.ENTER_FRAME, startGame);
addEventListener(Event.ENTER_FRAME, collision);
addEventListener(Event.ENTER_FRAME, coneCollision);
function startGame(evt:Event):void {
if (started == false) {
spawnHazard();
}
}
function spawnHazard() {
started = true;
therespawn = new RespawnObject();
addChild(therespawn);
thecone = new trafficcone();
addChild(thecone);
therespawn.x = -50;
therespawn.y = 310;
thecone.x = 600;
thecone.y = 310;
}
function collision(evt:Event):void {
thecone.x -= 15;
if(thecone.hitTestObject(therespawn)) {
thecone.x = 600;
}
}
Now the only way to finish the game or end it is to get hit by an obstacle which ive shown down below:
function coneCollision(evt:Event):void {
if(MainChar.hitTestObject(thecone)) {
gotoAndStop("frameFive");
}
}
Everytime the highscore frame appears the cone is still spawning and despawning, why is that?
I haven't declared them as global?
Any help appreciated, thanks!
You can fix your problem by setting started to false:
function coneCollision(evt:Event):void {
if(MainChar.hitTestObject(thecone)) {
started = false;
gotoAndStop("frameFive");
}
}
The flash timeline really only has to with the way MovieClips are visually displayed as children of the stage. Removing an object from the timeline doesn't just suddenly nullify all the code associated with that object. In other words, your ENTER_FRAME method still runs in the background even if the object is no longer a child of the Stage, regardless of the frame number for the MovieClip. If you're serious about coding you might consider investigating in Classes and Object Oriented AS3. Classes are much nicer to work with than the Flash timeline.

How to reset Accelerometer Event As3

In my project, at the start of game accelerometer event works fine. While game reaches the game over page and click on restart button. All the objects are working good when restarting the whole game also all values have been reset but accelerometer is not working.
Thanks in Advance.
The code follows:
if (Accelerometer.isSupported)
{
acc = new Accelerometer();
acc.addEventListener(AccelerometerEvent.UPDATE,updateFn);
}
public function updateFn(e:AccelerometerEvent):void
{
targetX = e.accelerationX * 9.8;
}
Just register the Accelerator once the app was launched/activated and save it's value to global variables on each accelerator update and disabled it each time the app is put to the background / deactivated / exit. Eg:
NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleApplicationDeactivated);
NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleApplicationActivated);
function handleApplicationActivated( e:Event):void {
// Check if Accelerometer is already activated
if( acc != null ) return;
acc = new Accelerometer();
acc.addEventListener(AccelerometerEvent.UPDATE,update);
}
function update( e:AccelerometerEvent ):void {
GlobalVars.accX = e.accelerationX;
GlobalVars.accY = e.accelerationY;
GlobalVars.accZ = e.accelerationZ;
}
function handleApplicationDeactivated( e:Event):void {
acc.removeEventListener(AccelerometerEvent.UPDATE,update);
acc = null;
}
edit: you might will want to use this activate/deactivate code instead : NativeApplication DEACTIVATE-ACTIVATE app when in the background since the NativeApplication have some issues.

TouchEvent.TOUCH_MOVE not fired on MS Surface

I'm developing an AIR app prototype on Surface, and I'm noticing that TouchEvent.TOUCH_MOVE is not fired as expected....
I've built a quick debug application:
private var tf:TextField;
public function init():void{
trace("init");
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
tf = new TextField();
tf.multiline = true;
tf.width = 800;
tf.height = 2000;
this.stage.addChild(tf);
tf.text = "Test";
var touch:Sprite = new Sprite;
touch.graphics.beginFill(0xFFFF00,0.3);
touch.graphics.drawRect(0,0,800,200);
touch.graphics.endFill();
this.stage.addChild(touch);
touch.addEventListener(TouchEvent.TOUCH_BEGIN, updateTF);
touch.addEventListener(TouchEvent.TOUCH_END, updateTF);
touch.addEventListener(TouchEvent.TOUCH_MOVE, updateTF);
touch.addEventListener(TouchEvent.TOUCH_OVER, updateTF);
touch.addEventListener(TouchEvent.TOUCH_TAP, updateTF);
touch.addEventListener(TouchEvent.TOUCH_ROLL_OUT, updateTF);
touch.addEventListener(TouchEvent.TOUCH_ROLL_OVER, updateTF);
}
private function updateTF(event:TouchEvent):void
{
trace(event);
tf.appendText("\n event:" + event);
}
...and it confirms me no TouchEvent.TOUCH_MOVE is being fired.
What could be the issue?
Thank you
So, I didn't solve the issue but I found the explication and a workaround.
This is bug on AIR 3.8 on Windows 8.
Just install a previous version of AIR.
Hope it helps someone :)
You can use MouseEvents instead of touch events for Microsoft Surface. Because adobe not yet released for Microsoft Surface touch device(see adobe labs).
Normally all touch events fall back to mouse events so you can try with Mouse-events. It works well for me . But not sure about Mouse Move event but it works for touch begin and touch end events to mousedown and mouseup events.