I'm planning to make a digital magazine with adobe flash cc 2014. I have different scenes for different page and I use Gesture_SWIPE to navigate the magazine.
The problem is, I only write the script in 1 scene but it worked for all the scenes. So I can do the swipe in all scenes.
While I want each scenes have a different action such as only page 1 that can't be swiped to the left, only page 2 can be swiped up and down and etc
I did use the if else in each case, it worked for the swiped left, but not working for the swipe right.
Can you please help me? How can I make this scripts work only for 1 scene?
This is my gesture swipe code:
import flash.events.TransformGestureEvent;
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE, SwipeHandler);
function SwipeHandler(event:TransformGestureEvent):void
{
switch(event.offsetX)
{
case 1: //swiped right
{
prevScene();
break;
if(this.currentScene.name == "Scene 1") //THIS ONE DIDN'T WORK
{
stop();
}
}
case -1: //swiped left
{
if(this.currentScene.name == "Scene 12") //THIS ONE WORK
{
stop();
}
else
{
nextScene();
break;
}
}
}
}
I had a little trouble understanding your problem and requirements but a few things to continue with:
your first case should probably work in the same way as the 2nd
your breaks won't be always fired so will sometimes flow onto the next case the way your statement were setup.
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE, SwipeHandler);
function SwipeHandler(event:TransformGestureEvent):void {
switch(event.offsetX) {
case 1:
if(this.currentScene.name == "Scene 1") {
stop();
} else {
prevScene();
}
break;
case -1:
if(this.currentScene.name == "Scene 12") {
stop();
} else{
nextScene();
}
break;
default:
//something else happened
}
}
Related
For my games I allways use singletouch so I allways keep track of the Pointer in an onTouch() Method like this:
-get the pointer Id at (0) with event.getPointerId(0); when pId==0, where pId is the pointer Id
-there will be no computation of other pointers when the pId!=-1(standard value I assign when the prime pointer is lifted up again.) and if the current pointer index is not the same as the pointer indes of the pointer Id I have saved in pId
like so:
switch(e.getAction())
{
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_DOWN:
if(pID!=-1)break;
...
pID=e.getPointerId(0);
...
break;
case MotionEvent.ACTION_MOVE:
if(pID==-1||e.getActionIndex()!=e.findPointerIndex(pID))break;
...
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
if(pID==-1||e.getActionIndex()!=e.findPointerIndex(pID))break;
...
pID=-1;
...
break;
}
But now I stumbled across a code that I wrote 2 weeks ago where I do it other way, because I wanted to react to Multitouch(maximum two touch pointers allowed for zooming):
switch(e.getAction())
{
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_DOWN:
if(tP1.getPointerIndex()==-1&&tP2.getPointerIndex()==-1)
{
tP1.setPointerIndex(currentPointerIndex);
mode=Mode.SINGLE;
}
else if(tP1.getPointerIndex()!=-1&&tP2.getPointerIndex()==-1&&!isDragging)
{
tP2.setPointerIndex(currentPointerIndex);
mode=Mode.MULTI;
}
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.X=e.getX();
tP1.Y=e.getY();
tP1.oldX=tP1.X;
tP1.oldY=tP1.Y;
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.X=e.getX();
tP2.Y=e.getY();
tP2.oldX=tP2.X;
tP2.oldY=tP2.Y;
}
else break;
...
break;
case MotionEvent.ACTION_MOVE:
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.X=e.getX();
tP1.Y=e.getY();
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.X=e.getX();
tP2.Y=e.getY();
}
else break;
...
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.X=e.getX();
tP1.Y=e.getY();
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.X=e.getX();
tP2.Y=e.getY();
}
else break;
if(mode==Mode.SINGLE)
{
mode=Mode.NONE;
tP1.resetPointerIndex();
}
else if(mode==Mode.MULTI)
{
mode=Mode.SINGLE;
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.resetPointerIndex();
tP1.setPointerIndex(tP2.getPointerIndex());
tP1.X=tP2.X;
tP1.Y=tP2.Y;
tP1.oldX=tP2.oldX;
tP1.oldY=tP2.oldY;
tP2.resetPointerIndex();
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.resetPointerIndex();
}
}
break;
}
tP1 and tP2 ar class objects who keep track of the last x and y coordinates and of the pointer index. So they represent the two pointers. I allways swap the second pointer on the first pointer if the first pointer is raised up and the second is still on. Now it is really embarrassing because I wrote this code for a colleague of mine for a few bucks.
So I jsut keep track of the indices of the pointers but they are sometimes changing. I didnt noticed it because everything was working fine. But now as I came accross it I noticed this. What should I do. Do I need to change it or do you see no problem to let this code be as it is?
I am just assuming that it can go wrong because I dont track the pointer Id but only the pointer Index.
Also I never experience it when testing that for example the primary pointer when first touched get swapped to another index but 0 when another touchpointer comes down.
I feel so dumb that I didnt notice it, so now I need a good advice how to behave pls help...
It is really strange that is code worked because I do this:
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
So Android not only assigns the index of the first pointer with 0 but also the pointerId with 0. This code is a mess i think
I have an application about pmt function. However there are so many conditions that need to be handled. Somehow the app will not work with having more than 12 if-else. I want to use switch case, but i still not really understand how to use switch case(been 1 and half month since my 1st try using eclipse).Any example will be highly appreciated.
here is my example code:
if(String1.toString().equals("condition1")){
//do something
if(String2.toString().equals("condition1.1")&& String3.toString().equals("condition1.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition1.##")&& String3.toString().equals("condition1.##")){
//do something else
}
}
else if(String1.toString().equals("condition2")){
//do something
if(String2.toString().equals("condition2.1")&& String3.toString().equals("condition2.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition2.##")&& String3.toString().equals("condition2.##")){
//do something else
}
}
if(String1.toString().equals("condition3")){
//do something
if(String2.toString().equals("condition3.1")&& String3.toString().equals("condition3.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition3.##")&& String3.toString().equals("condition3.##")){
//do something else
}
}
and still keep going....to handle all possibilities .I am wondering, How to do this in switch case . Or a better implementation if we have 3 times 3 conditions. For example a,b,c(suppose these three conditions can only be used once) and d,e,f and g,h,i then condition 1 is a,d,g ; condition 2 is a,d,h condition 3 is a,d,i ; condition 4 a,e,g........on so on
Note:Suppose that the API version is 8-11 (old android)
thanks
The answer is dependent on your target version of android. From KitKat and upwards (API Level 19+), Java 7's switch (String) is available. I'd also strongly suggest trying to group the subcases (condition n.x) into different methods. It just gets very unwieldly quickly, otherwise:
switch (String1.toString) {
case "condition1":
handleCase1(String2, String3);
break;
case "condition2":
handleCase2(String2, String3);
break;
}
If that still results in too complex code, you can try a lookup table together with a command pattern:
class ConditionKey {
final String String1;
final String String2;
final String String3;
public int hashCode(); // hash strings
public boolean equals(); // compare strings
}
interface ConditionCommand {
// use whatever arguments the operation needs, you can also
// add fields and initialize in the constructor
void perform(final ConditionKey key, /* [...] */);
}
Map<ConditionKey, ConditionCommand> actionMap = new HashMap<>();
actionMap.put(
new ConditionKey("condition1", "condition1.1", "condition1.2"),
new ConditionCommand() {
void perform(final ConditionKey key) {
// perform actions that need to be done
}
}
);
And then instead of the if-else or switch-case:
[...]
ConditionKey key = new ConditionKey(string1, string2, string3);
// get the action from the map
ConditionCommand command = actionMap.get(key);
// perform the command
command.perform(key);
since java 1.7 switch on string is supported.
you could annidate two switch:
switch(String1) {
case "condition1": {
switch(String2) {
case "condition1.1":
break;
// ... other cases
default:
break;
}
}
break;
// ... other cases
default break;
}
I'm working on a music player for android.
In it I have an activity which contains ListView that displays songs/artists/albums etc.
I keep track of the lists the user views (For example all album --> artist x --> album y)
In order to provide a way for the user to go back in the lists, I override onBackPressed() so it will pick the previous list the user viewed (handled by a Stack)
#Override
public void onBackPressed()
{
if (ListActivity.onScreenList == ListActivity.ALL_SONGS && viewStack.empty())
super.onBackPressed();
else if (viewStack.empty())
navListView.performItemClick(navListView.getChildAt(0), 0, navListView.getChildAt(0).getId());
else
{
TypeAndName tan = viewStack.pop();
if (tan.getName() == null)
{
int num = tan.getType();
navListView.performItemClick(navListView.getChildAt(num), num, navListView.getChildAt(num).getId());
}
else
{
switch (tan.getType())
{
case ARTISTS:
break;
case ALBUMS:
{
Album alb = albumListAdapter.getAlbumAtPosition(position);
c = db.rawQuery(DatabaseHandler.qryGetSongsByAlbum, new String[] { alb.getAlbumName() });
songListAdapter.setPlaylist(DatabaseHandler.convertCursorToSongsArray(c));
lv.setAdapter(songListAdapter);
ListActivity.onScreenList = ListActivity.ALL_SONGS;
}
break;
case GENRES:
break;
case PLAYLISTS:
break;
case YEAR:
break;
// TODO add other options here
}
}
}
}
the weird part is, that the back sound (the one that is played whenever you press the back button) is played twice.
However, the onBackPressed() method is not called twice and the code works as expected.
I'm working with FlashBuilder 4.6 to develop Android app.
In the application there is a requirement of setting some values when device orientation changes. i.e. setting the values when the screen/device orientation changes from Landscape to Portrait and vice-verse.
Although Initially my application has LandScape orientation. And this requirement is on specific view.
I want every time when the screen/device orientation changes the values must be set there.
I am not getting the desired result.
Please guide me and tell me if I am at wrong in code or how can i achieve this.
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="{data}"
xmlns:mx="library://ns.adobe.com/flex/mx"
viewActivate="view1_viewActivateHandler(event)"
creationComplete="view1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void
{
if(Accelerometer.isSupported)
{
accl = new Accelerometer();
accl.addEventListener(AccelerometerEvent.UPDATE,update); //accl.addEventListener(AccelerometerEvent.UPDATE,adjustImage);
}
}
private function update(event:AccelerometerEvent):void
{
this.stage.autoOrients = true;
//in the below line I am attaching StageOrienationEvent that will adjust the
//values.
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE,adjust);
}
private function adjust(event:StageOrientationEvent):void
{
if(event.afterOrientation == StageOrientation.ROTATED_LEFT)
{
testVal.text ="After OR is LEFT";
}
else if(event.afterOrientation == StageOrientation.ROTATED_RIGHT)
{
testVal.text ="After OR is RIGHT";
}
else if(StageAspectRatio.LANDSCAPE)
{
testVal.text ="StageAspectRatio is Landscape";
}
else if(StageAspectRatio.PORTRAIT)
{
testVal.text="StageAspectRatio is Portrait";
}
}
</fx:Script>
Thanks.
Finally found the solution after spending two days on this.
in update function add OrientationChanging Event in stage.addEventListner.
private function update(event:AccelerometerEvent):void
{
this.stage.autoOrients = true;
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING,adjust);
}
private function adjust(event:StageOrientationEvent):void
{
switch(event.afterOrientation)
{
case StageOrientation.DEFAULT:
//Do Something here. The Portrait Position.
break;
case StageOrienatation.ROTATED_LEFT:
//Do Something here when you rotate your phone portrait to left side.
break;
case StageOrienatation.ROTATED_RIGHT:
//Do something here when you rotate your phone portrait to right side.
break;
}
}
Many thanks to all who viewed this question and try put their efforts for solution. :)
i am not a experimented coder! but : private function update(event:AccelerometerEvent):void should be : private function accl(event:AccelerometerEvent):void ?? or something like this in below code if(stage[not event].afterOrientation == ??? its just a opinion!
I am trying to make a donation menu for my app. I have figured out the part that when a user clicks donate, more buttons come up saying how much. Now, I want to be able to have the amount buttons go away if they click the same button again. I want the regular DonateButton to remain. How would I come about doing that?
I have already set it as invisible using purchaseButton.setVisibility(View.GONE);
Here is the code for clicking the button and the other buttons appearing:
public void onClick(View v) {
switch (v.getId()) {
case R.id.DonateButton:
purchaseButton.setVisibility(View.VISIBLE);
purchaseButton2.setVisibility(View.VISIBLE);
purchaseButton3.setVisibility(View.VISIBLE);
case R.id.Donate:
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "donate");
// android.test.purchased or android.test.canceled or android.test.refunded
} else {
Log.i(TAG,"Can't purchase on this device");
}
break;
default:
// nada
Log.i(TAG,"default. ID: "+v.getId());
break;
case R.id.Donatetwo:
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "donate2");
// android.test.purchased or android.test.canceled or android.test.refunded
} else {
Log.i(TAG,"Can't purchase on this device");
}
break;
case R.id.Donatethree:
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "donate3");
// android.test.purchased or android.test.canceled or android.test.refunded
} else {
Log.i(TAG,"Can't purchase on this device");
}
break;
}
}
Screenshot of what I mean:
IMAGE URL (DON'T HAVE 10 REPUTATION YET):
http://i.stack.imgur.com/AMdhS.png
What I am trying to say is.
The app comes up just showing the "Donate!" Button. =>
The user clicks the "Donate!" Button. =>
The buttons "Donate $1", "Donate $3", and "Donate $5" appear. =>
I NEED HELP FROM HERE
A user wants to close the "Donate $1", "Donate $3", and "Donate $5" Buttons. =>
To close them, they click the "Donate!" which was the button they used to open it all. =>
The "Donate $1", "Donate $3", and "Donate $5" go away.
I want it to still allow them to open and close those buttons more than once though.
a simple state variable should do.
put this in your field definition area:
boolean areButtonAmountVisible = false;
and this code as your onClick():
case R.id.DonateButton:
if( areButtonAmountVisible )
{
areButtonAmountVisible = false;
purchaseButton.setVisibility(View.GONE);
purchaseButton2.setVisibility(View.GONE);
purchaseButton3.setVisibility(View.FONE);
}
else
{
areButtonAmountVisible = true;
purchaseButton.setVisibility(View.VISIBLE);
purchaseButton2.setVisibility(View.VISIBLE);
purchaseButton3.setVisibility(View.VISIBLE);
}
Try this.
Use getVisibility() method to know the visible state of button.
int visibility;
visibility = button.getVisibility();
if(visibility == View.VISIBLE) {
button.setVisibility(View.INVISIBLE);
} else {
button.setVisibility(View.VISIBLE);
}
You have to check like this for every button.
I will suggest one more thing.
Take a global variable and use it as notifier, use below code in all button click functions.
boolean again = false;
if (again) {
// make all invisible
again = false;
} else {
// make all visible
again = true;
}