recognizing button presses in actionscript - actionscript

In a mini flash game, I have a few different level select buttons, and they all attach to one "levelChange()" function, and I'm just wondering if there is an attribute that stores which button was pressed or how to determine which was pressed if not.
Thanks

try the currentTarget property of MouseEvent example:
function buttonClick(event:MouseEvent):void
{
trace(event.currentTarget);
}
I recommend you store your buttons in variables outside your function like so:
var levelOne:MovieClip = levelOne;
So you can later call upon them like this:
function buttonClick(event:MouseEvent):void
{
if (event.currentTarget == levelOne) {
trace("level one selected");
else if (event.currentTarget == levelTwo) {
trace("level two selected");
}
}

If you're using AS3 you can add a switch statement in your levelChange() function.
For example.... if you have 2 buttons, one with the instance name of "level1", and the other "level2".
function levelChange( me:MouseEvent ):void
{
switch( me.currentTarget.name )
{
case "level1":
// Go to Level 1 here.
case "level2":
// Go to Level 2 here.
}
}

Related

Check for condition befor executing button selector

I have multiple buttons with different functions, now these buttons should check for a condition befor executing the actual functionality, I don't won't to change all buttons functions like the following:
#objc func btnAction(sender: UIButton) {
if condition {
runSomthing()
return
} else {
run the actual btn action
}
}
I need to extract this to a superclass that accomplishes what I looking for without changing the current button actions
Is there a correct way to do it?
One of the solutions is to link all the buttons to the same IBAction and perform corresponding button functionalities using the button tag value. This prevents the condition checking in each IBAction.
guard let button = sender as? UIButton else {
return
}
if condition {
runSomthing()
return
} else {
switch button.tag {
case 1:
Perform operation A
case 2:
Perform operation B
case 3:
Perform operation C
default:
print("None")
return
}
}
Enums can also be used instead of tag value for better identification. I referred How to use one IBAction for multiple buttons in Swift?

Detect what type of event triggered a UIControl's action

I have a custom UIControl subclass with an action method callback. I want to display the value of the control element on a UILabel while it is being adjusted, and then I want the label to become hidden when the user stops adjusting the control.
Therefore, I have connected the action for both UIControlEventValueChanged and UIControlEventTouchUpInside. Both successfully invoke my action method. However, to do different things in this method based on the action I need to know which event triggered the method. How can I do this? I've looked through UIControl and don't see an obvious property. state seems to return 1 for both actions.
So something like this:
- (void)handleSlider1:(CustomSlider*)sender {
if (sender.state == UIControlEventValueChanged) {
// code
} else {
// different code
}
}
You can distinguish the two events pretty easily by connecting them each to separate IBActions. Each new action then would call your single handler, passing the appropriate UIControlEvent value along:
- (IBAction)sliderValueChanged:(CustomSlider *)slider
{
[self handleSlider1:slider forEvent:UIControlEventValueChanged];
}
- (IBAction)sliderValueChanged:(CustomSlider *)slider
{
[self handleSlider1:slider forEvent:UIControlEventTouchUpInside];
}
- (void)handleSlider1:(CustomSlider *)sender forEvent:(UIControlEvents)event
{
if (event == UIControlEventValueChanged)
//...
}
If you want to use the same method to handle both events then you can check the highlighted property of the control:
if (sender.highlighted) {
// slider is changing value (value changed)
} else {
// slider has stopped changing value (touch up inside)
}
Alternatively, you could simply create two separate action methods and connect each event to the required method.

Is it possible to disable chosen.js for a specific select box?

Given a certain event (say, a button click), I'd like to disable Chosen.js for a specific select box.
Is this possible?
I had a similar requirement although it didn't require a button click, I just wanted to enable / disable Chosen for specific select boxes.
My solution is for each select element that you don't want to use Chosen with, add the CSS class 'no-chzn'. Then in chosen.jquery.js, add the following lines to the constructor (this is near line 533 in version 1.1.0):
$.fn.extend({
chosen: function(options) {
if (!AbstractChosen.browser_is_supported()) {
return this;
}
return this.each(function(input_field) {
var $this, chosen;
$this = $(this);
if ($this.parent().hasClass("chosen-disable")) { return; } // Just add this line!
chosen = $this.data('chosen');
if (options === 'destroy' && chosen) {
chosen.destroy();
} else if (!chosen) {
$this.data('chosen', new Chosen(this, options));
}
});
}
});
For example you have a select with class no-chosen and you do this:
$("select:not('.no-chosen')").chosen();
It means it takes all selects except the one with no-chosen class.

Windows Phone - XNA game - Back button

I have some problem with my back button. In my game I have two screens. One with title (menu) and second with the game. When I use once back button i pause game and back to title screen. When i use it again I need to kill the app process. How can i do that? Down below i show u how I use back button. I try use 2 gestures, but when I declared them nothing go right.
That's how i declared gesture in Initialize():
TouchPanel.EnabledGestures = GestureType.FreeDrag
First I declared
bool IsPlayingGame = true;
int endGame= 0;
Then in function Update:
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
if (!IsPlayingGame) this.Exit();
}
if (isTitleScreenShown)
{
UpdateGameScreen();
}
else if (isGameSceenShown)
{
UpdateTitleScreen();
// TODO: Add your update logic here
/* MY FUNCTIONS */
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
if (endGame == 5) base.Exit();
}
}
base.Update(gameTime);
In Visual studio, it works. I thought great, but on Nokia phone it doesn't. Why? Any help?
In the GamePage.xaml.cs there should be a method protected override void OnNavigatedFrom(NavigationEventArgs e)
This method is triggered when you touch the 'back' button on your device.

Best method to determine the sender of an event

I have a simple question about event handling in iOS applications... suppose you have an app with some buttons that react to the TouchUpInside event calling the same action, what is the best way within the action method to understand what is the button that triggered the event? I know that it can be easily done using the title of the button, but I think it is not the best way if you have a localized app in which button text may change (unless it is possible to reverse the localization of the title, i.e. retrieve the original string from a localized string)... is there a good practice about this topic? Should I use some other property of buttons to distinguish among different buttons?
Thank you in advance for any help.
There is something called a "Tag" that you can set for UIButtons, or anything that can respond to an event for that matter. If you are using Interface Builder, click the attributes inspector for the item and select a value for the tag (integer). In your code do something like this...
...
- (IBAction)buttonReceived:(id)sender
{
if ([sender tag] == 1) {
//Do something
}
else if ([sender tag] == 2) {
//Do something else
}
}
In addition to the tag property, or just in case you are already using the tag for some other purpose that would mean duplicate tag values for one or more different buttons, you can always set up an IBOutlet ivar to each button you needed to check, and then in the IBAction, do something like this:
- (IBAction)buttonReceived:(UIButton *)sender
{
if (sender == myButtonA) {
// processing for button A
}
else if (sender == myButtonB) {
// processing for button B
}
}
It is a bit more work, but it can come in handy at times.

Resources