How to reset Accelerometer Event As3 - actionscript-3

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.

Related

TouchEvent.TOUCH_BEGIN, onTouchBegin Freezes after several Reloads

I am building an Adobe Air AS3 IOS and Android App, in which i have a movie clip in the center of the stage. When you start touching this movie clip, you can move it all around the stage.
This is how i'm doing so :
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
MC_M1.alpha = 1;
MC_M1.addEventListener(Event.ENTER_FRAME, ifHitAct);
MC_M1.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
MC_M1.x = 0.516 * gameIntro.stageWidthToUse;
MC_M1.y = 0.75 * gameIntro.stageHeightToUse;
MC_M1.height = 0.2 * gameIntro.stageHeightToUse;
MC_M1.width = MC_M1.height / 1.4;
gameIntro.STAGE.stage.addChildAt(MC_M1,1);
function onTouchBegin(event:TouchEvent)
{
trace("TouchBegin");
if (touchMoveID != 0)
{
trace("It Did Not");
return;
}
touchMoveID = event.touchPointID;
gameIntro.STAGE.stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
gameIntro.STAGE.stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
}
function onTouchMove(event:TouchEvent)
{
if (event.touchPointID != touchMoveID)
{
return;
}
//trace("Moving")
MC_M1.x = event.stageX;
MC_M1.y = event.stageY;
}
function onTouchEnd(event:TouchEvent)
{
if (event.touchPointID != touchMoveID)
{
return;
}
//trace("Ending");
touchMoveID = 0;
gameIntro.STAGE.stage.removeEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
gameIntro.STAGE.stage.removeEventListener(TouchEvent.TOUCH_END, onTouchEnd);
}
When the player actually looses the game, what i am actually doing is the following :
MC_M1.removeEventListener(Event.ENTER_FRAME , ifHitAct);
MC_M1.removeEventListener(TouchEvent.TOUCH_BEGIN , onTouchBegin);
gameIntro.STAGE.stage.removeChild(MC_M1);
MC_M1.alpha = 0;
isDead = 1;
replayButToUse.x = 0.127 * gameIntro.stageWidthToUse;
replayButToUse.y = 0.91 * gameIntro.stageHeightToUse;
replayButToUse.addEventListener(MouseEvent.CLICK, gotoIntro);
This is all happening in a class called : introClassToUse.
So when the users looses, he will get a replay button, and when he clicks it, he will go back to the same class and reload everything, using the following code :
function gotoIntro(event:MouseEvent):void
{
replayButToUse.removeEventListener(MouseEvent.CLICK, gotoIntro);
replayButToUse.alpha = 0;
replayButToUse.removeEventListener(MouseEvent.CLICK, gotoIntro);
stop();
var reload:introClassToUse = new introClassToUse();
}
And so everything loads back up and the game restarts. My problem is, i'm facing a very weird behavior when i tend to replay the game more than 2-3 times. The MC_M1 just stops listening to any touch event, but keeps on listening to ENTER_FRAME events, in which i keep touching the MC_M1 but it seems to not respond to it. I even debugged it remotely from my iPhone, for the first couple of replays, i can see the trace("TouchBegin"); with it's outcome, it was showing me TouchBegin, but after a few replays, the touch events just froze. What am i missing?
Any help is really appreciated, i'm new in AS3, i need to learn so i could manage more
Edit 1 :
I have no code on any frame, i just have lots of AS Classes.
The fla file is linked to an AS Class called gameIntro. In this class, i have linked the following :
- STAGE is an object of type Stage.
- gameIntro.STAGE = stage
Later on, when the user clicks a play button, i call the class introClassToUse. This class has all the game functionalities. All the code present above is in introClassToUse. When the user looses and clicks the replay button, he will go to "goToIntro" function, im which i recall the introClassToUse.
It's all working fine, with several other timers implemented and all, the only problem is that after several replays, the MC_M1 just freezes over
I am removing the MC_M1 each time the user looses and re-add them when i call back the introClassToUse, because i tried to use the .visible property, it didn't work at all ( this is why i am using the gameIntro.STAGE.stage.removeChild(MC_M1)
I know the question is old but maybe someone is still wondering what is going on here (like me).
There are lot of problems in you code but I thing the root of your problem starts here:
function gotoIntro(event:MouseEvent):void{
//...
var reload:introClassToUse = new introClassToUse();
}
It is usually unwanted behavior if simply creating an instance does more than nothing to your program and you don't even need to assign it to variable in this case.
You mentioned this code is located in your introClassToUse class. This basically means that you are creating new instance of your game inside old one and this seem to be completely awry.
You should consider using only instance properties in your class definition and create new introClassToUse() in external classes;
You didn't include many important details about your code like
How the whole class structures look like - for example you can't place line like MC_M1.addEventListener(Event.ENTER_FRAME, ifHitAct);in the scope of your class so obviously you have this in some function and we don't know when and from where it is called.
Where and how your variables are declared, and assigned. It's hard to tell if your MC_M1 is property of an instance or a class, is it internal/public/private/...
Do you link library symbols to your classes or acquire it from stage.
There could be many things that could give you such result. Based on what you wrote I've reproduced behavior similar to what you've describe but using mouse event and a dummy loose condition. This ends the game each time you drop the mc partially outside right edge of the sage, show restart button and starts again if you click it (basically it's mostly your code). It works fine for about 10s and than suddely you can't move the mc anymore. The frame event is still tracing out but touch/mouse is not.
How can it be? I suspect that you could remove only listeners somewhere and have invisible mc stuck on the new one. And this could be easy overlooked, especially if you using static properties. Again we don't even know where is your movie clip coming from so we can only guess what is happening whit your code but I've tried to take the example simple this is how I did it. The problem may lay in some completely different place but you can guess for all scenarios.
Document class of the project - GameIntro.as
package
{
import flash.display.Sprite;
public class GameIntro extends Sprite
{
//Document class. this need to be compiled with strict mode off.
public function GameIntro() {
GameIntro.STAGE = stage;
GameIntro.stageWidthToUse = stage.stageWidth;
GameIntro.stageHeightToUse = stage.stageHeight;
var intro:IntroClassToUse = new IntroClassToUse();
stage.addChild(intro);
}
}
}
IntroClassToUse.as
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
/**
* You need to have library symbol linked to this class in .fla with two mcs -
* mcFromLibrarySymbol (dragable) and repButton (reapatButton)
*/
public class IntroClassToUse extends MovieClip
{
var t = 0; //timer ticks
var fc:uint = 0; //frames counter
var isDead = 0;
var mc;
static var repButton;
var logicContex:Timer = new Timer(30);
public function IntroClassToUse() {
trace("toUse", GameIntro.stageWidthToUse);
mc = mcFromLibrarySymbol;
if(!repButton) repButton = repButtonX;
logicContex.addEventListener(TimerEvent.TIMER, logicInterval);
logicContex.start();
init();
}
internal function init() {
trace("init");
mc.alpha = 1;
mc.addEventListener(Event.ENTER_FRAME, onFrame);
mc.addEventListener(MouseEvent.MOUSE_DOWN, onMDown);
mc.x = 0.516 * GameIntro.stageWidthToUse;
mc.y = 0.75 * GameIntro.stageHeightToUse;
mc.height = 0.2 * GameIntro.stageHeightToUse;
mc.width = mc.height / 1.4;
GameIntro.STAGE.stage.addChildAt(mc, 1);
}
internal function onLoose() {
trace("onLoose");
mc.removeEventListener(Event.ENTER_FRAME , onFrame);
mc.removeEventListener(MouseEvent.MOUSE_DOWN, onMDown);
GameIntro.STAGE.stage.removeChild(mc);
mc.alpha = 0;
isDead = 1;
repButton.x = 0.127 * GameIntro.stageWidthToUse;
repButton.y = 0.91 * GameIntro.stageHeightToUse;
repButton.addEventListener(MouseEvent.CLICK, onReplay);
repButton.alpha = 1;
}
internal function onReplay(e:MouseEvent):void {
trace("onReplay");
repButton.removeEventListener(MouseEvent.CLICK, onReplay);
repButton.alpha = 0;
stop();
new IntroClassToUse();
}
internal function onMDown(e:MouseEvent):void {
trace("mouseDow");
GameIntro.STAGE.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMMove);
GameIntro.STAGE.stage.addEventListener(MouseEvent.MOUSE_UP, onMUp);
}
internal function onMMove(e:MouseEvent):void {
mc.x = e.stageX;
mc.y = e.stageY;
}
//you loose the game if you release you mc with part of it over rigth stage edge.
internal function onMUp(e:MouseEvent):void {
trace("mouseUp");
GameIntro.STAGE.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMMove);
GameIntro.STAGE.stage.removeEventListener(MouseEvent.MOUSE_UP, onMUp);
trace("Stage:", GameIntro.STAGE.numChildren);
if (mc.x + mc.width > GameIntro.STAGE.stageWidth) onLoose();
}
internal function onFrame(e:Event):void {
trace("frames", fc++);
}
internal function logicInterval(e:TimerEvent):void {
if (t++ < 300 || !isDead) return;
init();
mc.alpha = 0;
mc.removeEventListener(MouseEvent.MOUSE_DOWN, onMDown);
isDead = 0;
}
}
}

Set object's `visible` to `true` when a `tap` event is performed then set it back to `false`

I have an object as MovieClip and I have a button as Button on my flash timeline.
When the button is tapped, I want to set the object.visible to true then when the button is not tapped, I want to set it back to false.
How can I do that?
I have tried this code, but it won't works as I want. I only can show the object but cannot hide it back.
button1.addEventListener(TouchEvent.TOUCH_TAP, touchTap);
function touchTap(e:TouchEvent): void {
mcObj.visible = true;
stage.addEventListener(TouchEvent.TOUCH_END, touchEnd);
}
function touchEnd(e:TouchEvent): void {
mcObj.visible = false;
stage.removeEventListener(TouchEvent.TOUCH_END, touchEnd);
}
I think this code could work.
button1.addEventListener(TouchEvent.TOUCH_BEGIN, touchTap);
function touchTap(e:TouchEvent): void {
mcObj.visible = true;
button1.addEventListener(TouchEvent.TOUCH_END, touchEnd);
}
function touchEnd(e:TouchEvent): void {
mcObj.visible = false;
button1.removeEventListener(TouchEvent.TOUCH_END, touchEnd);
}
I changed
1: TouchEvent.TOUCH_TAP to TouchEvent.TOUCH_BEGIN
2: stage.addEventListener to button1.addEventListener
Before saying anything about your problem, let's take a look on the definitions of the TouchEvent.TOUCH_BEGIN, TouchEvent.TOUCH_END and TouchEvent.TOUCH_TAP events :
The TouchEvent.TOUCH_BEGIN is :
Dispatched when the user first contacts a touch-enabled device ...
The TouchEvent.TOUCH_END is :
Dispatched when the user removes contact with a touch-enabled device ...
The TouchEvent.TOUCH_TAP is :
Dispatched when the user lifts the point of contact over the same InteractiveObject instance on which the contact was initiated on a touch-enabled device ...
And with some tests, we can see that the TouchEvent.TOUCH_END event is, in almost cases, fired before the TouchEvent.TOUCH_TAP one (by 1 or 2 milliseconds), so we can understand the we are able to detect if the user has already removed contact with the device (TouchEvent.TOUCH_END is fired) then if that was on the same InteractiveObject object on which the contact was initiated (TouchEvent.TOUCH_TAP is fired).
And that's why your code is not working.
Now, let's see your problem : you want to show a MovieClip just when your user tap a button and hide it when he releases that button but only for a very short time (the time of a tap ~= 300 milliseconds).
In this case, I recommend you to use a TouchEvent.TOUCH_BEGIN event listener with a timeout to hide that object even if your user didn't release the button.
For that, take this example :
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
btn.addEventListener(TouchEvent.TOUCH_BEGIN, on_touchBegin);
function on_touchBegin(e:TouchEvent): void
{
obj.visible = true;
hide_obj();
}
function hide_obj(): void
{
// you can use a Timer object instead of setTimeout()
var timeout:int = setTimeout(function(){
clearTimeout(timeout);
obj.visible = false;
}, 300);
}
Hope that can help.

How to restart an SWF with a click of a button?

I have created a drag and drop mini-game and, once finished, I wish for the user to be able to click a "try again" button and have the whole thing start over.
I have read in to this and see that there are a couple of options but am stuck as to which would be best for me.
I have created an FLA with all of my library items, an AS file (MainGame.as) with the main game and all of it's functions in one class and a second AS file (MyGame.as) which calls the class file to play the game.
Which would work best for me?
I have nothing on the layers and just cannot figure out how to remove the swf and load it again with the click of a button.
Am I right in thinking that I would add the button to the MyGame file in the timerDone function?
If so, how would I use that to reload the SWF from the start?
Here is my timerDone function ...
function timerDone(e:TimerEvent=null):void
{
if (countDown == 0)
{
count = 0;
finalScore = score;
}
else
{
count = (30) - (myTimer.currentCount);
finalScore = (count * 10) + (score);
}
myText.text = "GAME OVER!";
myText.x = 195;
displayText();
myText2.text = "Your score = " + (finalScore);
displayText2();
}
I am thinking that I add the button after the last line and somehow refresh the SWF, is that right?
Or am I way off?
You should do something like that: you call the function toReplay at the end of your function timerDone:
Function timerDone, after "displayText2();"
tryAgain.addEventListener(MouseEvent.CLICK, toReplay);
Outside the function timerDone
/**
* The function toReplay calls the function init which
* initializes the Timer and all your variables.
**/
function toReplay(e:MouseEvent):void {
e.target.removeEventListener(MouseEvent.CLICK, toReplay);
init();
}
function init():void {
// Timer
count = 0;
finalScore = 0;
myText.text = "";
// etc.
}
And the function init can directly be invoked to initialize all your variables at the begining of the game:
init();

Geolocator position changed event

I am developing a running tracker/pedometer app, I am using geolocator for the same,I am keeping the movement threshold property of the geoLocator to 10 here is my piece of code.
button click event
private void StartButton_Click(object sender, RoutedEventArgs e)
{
myLocator = new Geolocator();
myLocator.DesiredAccuracy = PositionAccuracy.Default;
myLocator.MovementThreshold = 10;
myLocator.ReportInterval=500;
myLocator.PositionChanged += myGeoLocator_PositionChanged;
_startTime = System.Environment.TickCount;
_timer.Start();
}
void myGeoLocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
var coord = new GeoCoordinate(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude);
if (_line.Path.Count > 0)
{
var previousPoint = _line.Path.Last();
distance += coord.GetDistanceTo(previousPoint);
var millisPerKilometer = (1000.0 / distance) * (System.Environment.TickCount - _previousPositionChangeTick);
_kilometres += Math.Round(distance, 2);
distanceLabel.Text = string.Format("{0:f2} meters", _kilometres);
MessageBox.Show("Changed");
}
else
{
Map.Center = coord;
}
_line.Path.Add(coord);
_previousPositionChangeTick = System.Environment.TickCount;
});
}
The problem is that the position changed event is only getting called once, I am trying to debug the code in emulator by changing the location points but still the event do not get called. where am I doing wrong??
Your code will work on a real device. However, in order to test on the emulator, try by setting the DesiredAccuracy property to High.
From How to test apps that use location data for Windows Phone:
If your app uses the GeoCoordinateWatcher class, you have to specify a value of GeoPositionAccuracy.High in the constructor or in the DesiredAccuracy property of the class before you can test your app with the location sensor simulator. If you leave the accuracy at its default value of GeoPositionAccuracy.Default, the PositionChanged event doesn’t recognize position changes that occur in the location sensor simulator.
There is also another workaround, that consists on running the native Maps app, which seems to fix the problem:
Set a current location in the emulator.
Run your app. It reports the current location as Redmond.
Run the Maps application. It correctly goes to the location set in
step 1.
Run your app again. Now it uses the correct current location.
Source: http://social.msdn.microsoft.com/Forums/wpapps/en-US/c2cc57b1-ba1f-48fb-b285-d6cfbb8f393a/windows-phone-8-emulator-returns-microsofts-location-only

AS3 - make script wait for keystroke

I need my script to stop and wait until the 'enter' key is pressed, then continue, and if requirments are are not met, to go back and wait again.
its a login screen to a game i am working on, it needs to wait for the user to press enter, then check the provided credidentails and allow/deny them assces.
//log-in screen
loginframe = new Login_Frame();
addChild(loginframe);
LoginToServer();
function LoginToServer():Boolean
{
inputname = new TextField;
inputname.type = TextFieldType.INPUT;
inputname.border = true;
inputname.x = 200;
inputname.y = 200;
inputname.height = 35;
inputname.width = 545;
inputname.multiline = false;
inputname.text = "example";
loginframe.addChild(inputname);
inputpass = new TextField;
inputpass.type = TextFieldType.INPUT;
inputpass.border = true;
inputpass.x = 200;
inputpass.y = 300;
inputpass.height = 35;
inputpass.width = 545;
inputpass.multiline = false;
inputpass.text = "example";
loginframe.addChild(inputpass);
loginframe.addEventListener(KeyboardEvent.KEY_UP, hwndLogKeyboard);
while (!loginsucsess)
{
//do nothing *this halts the compiler, takes 30+ seconds for window to appear after execution*
//and causes the debugger to shut off (due to delay fault)
//so i have three problems
//1. this is clearly not an accepable way to do this
//2. this code dosnt work, without my debugger i cant fix it
//3. even if the code did work, this is a huge project, i cant be goin without my debugger
}
return true;
}
function hwndLogKeyboard(evt:KeyboardEvent):void
{
if (evt.keyCode == 13)
{
if ((inputname.text == "tyler") && (inputpass.text == "shadowcopy"))
loginsucsess = true;
}
}
I come from a C++ background where this solution would work just fine, but flash seems to have a problem with twidling its thumbs.
of course ive tryed asking Google, but the search results wernt anything even close to the required topic (me: AS3 wait for keypress - Google: Learn Flash OOP!) <-- no, bad Google.
thx in advance;
Tyler
Blocking UI is not optimal in any language, unless you're making a text console application.
Your implementation infinitely loops within your while() statement until max script execution timeout is reached.
Instead, use asynchronous design patterns:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
[SWF(percentWidth = 100, percentHeight = 100, backgroundColor = 0xefefef, frameRate = 60)]
public class LoginForm extends Sprite
{
protected var username:TextField;
protected var password:TextField;
public function LoginForm()
{
super();
// construct view
username = new TextField();
addChild(username);
password = new TextField();
addChild(password);
// listen for change events from the text fields
username.addEventListener(Event.CHANGE, loginChangeHandler);
password.addEventListener(Event.CHANGE, loginChangeHandler);
}
protected function loginChangeHandler(event:Event):void
{
if ((username.text == "tyler") &&
(password.text == "shadowcopy"))
{
// authentication verified - continue
}
}
}
}
When either of the text fields value changes, they are tested for the authentication credentials you have specified. If met, you can continue; otherwise, the application idles without any overhead.
Having a login button might be more inline with user experience.
Per debugging, from Flash Professional go to the "Debug" menu and select "Debug Movie".
From Flash Builder, right-click on the project application and "Debug as" or press the debug button from the toolbar. Built on Eclipse, you may find it more robust for code editing, debugging, and profiling.