Starling onClick method - actionscript-3

I'm using starling framework, to simulate onclick method I use this code:
if(e.getTouch(this).phase == TouchPhase.ENDED){
//Some code
}
It's OK, but it also fires, if the mouse is not over the button anymore, however I'd like it to dispatch only if it's over. Is there any way to achieve this?
thanks
in the code, "this" is a Sprite, It's kinda irrelevant thougth

According to the docs the interactsWith(target:DisplayObject) method of the TouchEvent class should return true if the target is currently being touched. I have no way to test this theory, but the following should work:
if (e.getTouch(this).phase == TouchPhase.ENDED && e.interactsWith(this)) {
//The touch ended on the same DisplayObject as it originated at
}

How about this idea:
if ( e.getTouch( this ).phase == TouchPhase.ENDED ) {
if ( this.hitTestPoint( stage.mouseX, stage.mouseY, true ) ) {
// Some code
}
}

The easy way would be to use a starling.display.Button to do it. they dispatch TRIGGERED Events, which are basically what you want. The 'not so easy' way is to keep track of your touches, by replicating what is actually done in Button :
private function onTouch(event:TouchEvent):void
{
var touch:Touch = event.getTouch(this);
if (!mEnabled || touch == null) return;
if (touch.phase == TouchPhase.BEGAN && !mIsDown)
{
//equivalent to MOUSE_DOWN
mIsDown = true;
}
else if (touch.phase == TouchPhase.MOVED && mIsDown)
{
// reset button when user dragged too far away after pushing
var buttonRect:Rectangle = getBounds(stage);
if (touch.globalX < buttonRect.x - MAX_DRAG_DIST ||
touch.globalY < buttonRect.y - MAX_DRAG_DIST ||
touch.globalX > buttonRect.x + buttonRect.width + MAX_DRAG_DIST ||
touch.globalY > buttonRect.y + buttonRect.height + MAX_DRAG_DIST)
{
mIsDown = false;
}
}
else if (touch.phase == TouchPhase.ENDED && mIsDown)
{
mIsDown = false;
//this is a click
dispatchEventWith(Event.TRIGGERED, true);
}
}
you'l have to change the buttonRect code to reflect your sprite's shape, but basically here you are.

Related

AS3 Custom Cursors Invisible

I've made a new question since it makes sense since the problem isn't the same anymore.
I'm making a colouring game.
CanvPark_mc is the Movieclip with the details of creating the canvas and where all the brush details are located. The Huge, Medium and SmallSelected1 variables, are variables that are changed when you click the brushes, so you're able to identify them.
I want to switch cursors depending on the brush that was last clicked. This can be done through the first if paramenters.
Right now, this is my code to switch, thanks to the help of #NBooo in the previous question
var cursor_mc: MovieClip = new MovieClip();
if (CanvPark_mc.HugeSelected1 == true) {
cursor_mc = cursor1_mc; //Big Cursor
removeChild(cursor_mc);
}
if (CanvPark_mc.MediumSelected1 == true) {
cursor_mc = cursor2_mc; //Medium Cursor
removeChild(cursor_mc);
}
if (CanvPark_mc.SmallSelected1 == true) {
cursor_mc = cursor3_mc; //Small Cursor
removeChild(cursor_mc);
}
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveCursor);
function moveCursor(myEvent: MouseEvent) {
if (CanvPark_mc.SmallSelected1 == false && CanvPark_mc.MediumSelected1 == false && CanvPark_mc.HugeSelected1 == false) {
Mouse.cursor = "auto";
} else if (cursor_mc){
addChild(cursor_mc);
setChildIndex(cursor_mc, this.numChildren - 1);
cursor_mc.x = stage.mouseX;
cursor_mc.y = stage.mouseY;
cursor_mc.mouseChildren = cursor_mc.mouseEnabled = false;
Mouse.hide();
}
}
The problem with this code, unfortunately, is that, whenever I click the said buttons, the cursor disappears. It doesn't change itself to any of the MovieClips I previously made.
Note that the removeChild in the if blocks is there in hopes of removing previous instances of cursor_mc on the screen after clicking them in the past.
In tests, it only changes, in the first if blocks, one of the parameters has a = instead of ==.
Example :
if (CanvPark_mc.HugeSelected1 == true) {
cursor_mc = cursor1_mc; // Big Cursor
removeChild(cursor_mc);
}
if (CanvPark_mc.MediumSelected1 == true) {
cursor_mc = cursor2_mc; //Medium Cursor
removeChild(cursor_mc);
}
if (CanvPark_mc.SmallSelected1 = true) {
cursor_mc = cursor3_mc; //This is the one he'll run, showing the smallest cursor
removeChild(cursor_mc);
}
In addition, if they are all reduced down to just =, the code will opt for the last if in the list.
I think there's a problem with my code and I can't tell what it is after hours of trying.
Could any of you help me figure this out?
I found the answer to my own question, so I'll be posting the code to help others out.
var cursor_mc: MovieClip = new MovieClip();
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveCursor);
function moveCursor(myEvent: MouseEvent) {
if (CanvPark_mc.HugeSelected1 == true) {
cursor_mc = cursor1_mc;
cursor1_mc.visible = true;
cursor2_mc.visible = false;
cursor3_mc.visible = false;
}
if (CanvPark_mc.MediumSelected1 == true) {
cursor_mc = cursor2_mc;
cursor1_mc.visible = false;
cursor2_mc.visible = true;
cursor3_mc.visible = false;
}
if (CanvPark_mc.SmallSelected1 == true) {
cursor_mc = cursor3_mc;
cursor1_mc.visible = false;
cursor2_mc.visible = false;
cursor3_mc.visible = true;
}
if (CanvPark_mc.SmallSelected1 == false && CanvPark_mc.MediumSelected1 == false && CanvPark_mc.HugeSelected1 == false) {
Mouse.cursor = "auto";
} else if (cursor_mc) {
addChild(cursor_mc);
setChildIndex(cursor_mc, this.numChildren - 1);
cursor_mc.x = stage.mouseX;
cursor_mc.y = stage.mouseY;
cursor_mc.mouseChildren = cursor_mc.mouseEnabled = false;
Mouse.hide();
}
}
The if block wasn't inside the function, so it wasn't correlating the code togheter. I put it inside as a test and it worked. Since when you click to change your mouse to a brush, the Movieclip starts following your mouse around. If you change to another brush, the movieclip gets left there. This is fixed by toggling it's visibility in the if's. :)
It's working as intended now.
Hope this helps someone!

how to disable movieclip?

I am using Adobe Flash CS5, Action Script 3.0. I want to disable my movie clip. How to do that?
My code:
var index:Array = ([0,1,2,3]);
var radioGroup1:RadioButtonGroup = new RadioButtonGroup("question1");
rb1.group = radioGroup1;
rb2.group = radioGroup1;
rb3.group = radioGroup1;
rb4.group = radioGroup1;
b1.addEventListener(MouseEvent.CLICK, submitclick,false);
function submitclick(event:MouseEvent):void
{
if (radioGroup1.selectedData == null)
{
b1.mouseEnabled = false;
//ExternalInterface.call("Test","Please selecte one answer");
//return;
} else {
if (radioGroup1.getRadioButtonIndex(radioGroup1.selection) == 2)
{
myscore += 10;
}
}
if (compquest>=maxquest)
{
gotoAndStop(1,"Scene 6");
}
else
{
MovieClip(this.root).gotoAndStop(1, "Scene " + questions[compquest++]);
}
}
I want to disable this b1 movieclip. So anyone could tell me what am I doing wrong?
If, by disable, you mean to make it unclickable and faded like a disabled button. Just reduce the alpha a bit and set its mouseEnabled property to false.
function submitClick(event:MouseEvent){
b1.alpha = .6;
b1.mouseEnabled = false;
}
This way the user will not be able to click it and it appear disabled.
First you could write, because event.target is p1:
function submitclick(event:MouseEvent):void
{
event.target.mouseEnabled = false;
}
On the other hand, the condition you want to evaluate in your if statement is probably false:
radioGroup1.selectedData == null
Therefore the associated code isn't executed:
b1.mouseEnabled = false;
You should test in your function and before your if statement:
trace(radioGroup1.selectedData == null);

else statement always run even the answer is true

tnx! it works. but when i tried to add another object named, p1_2 and add the "trace" thing to the code,
it goes back to the same problem.
p1_1.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
question.text = "shape?";
}
submit.addEventListener(MouseEvent.CLICK, onClickss);
function onClickss(e:MouseEvent):void
{
trace("ans.text = "+ans.text);
if (ans.text == "circle")
{
p1_1.visible = false;
}
else
{
gotoAndStop(6);
}
}
p1_2.addEventListener(MouseEvent.CLICK, onClick2);
function onClick2(e:MouseEvent):void
{
question.text = "Color?";
}
submit.addEventListener(MouseEvent.CLICK, onClickss2);
function onClickss2(e:MouseEvent):void
{
trace("ans.text = "+ans.text);
if (ans.text == "red")
{
p1_2.visible = false;
}
else
{
gotoAndStop(6);
}
}
what should i do.? do i nid to seperate p1_2 in another frame
and make another inputtextfield for it?
im planning to add 5 objects on the stage. until p1_5. -_-
Before the line...
if(ans.text == "circle") {
...add:
trace("ans.text = "+ans.text);
Clicking the submit button will trace the actual value of ans.text to the output panel when you test your movie in Flash. If you get a null reference error then ans has not been instantiated. Otherwise, knowing the actual value of ans.text should point you in the right direction to solve the problem.

Making an object or layer transparent when another object passes over it. (Flash Actionscript 3.0)

I'm very new to coding and am having trouble finishing the last little bit of a mini game type project for school.
So this is a draft of what the program looks like so far: http://aaronmillard.com/dir/wp-content/uploads/2013/08/Google-Doodle_Roomba.swf
Now what I want the program to do is "vacuum" up the google when the roomba drives over it. The easiest way to achieve this (I think) is to have an exact replica of the carpet layer without the google logo beneath the carpet layer with the google logo. So I would need to code something like "when the object(roomba) passes over object(carpetwithgooglelogo) make object(carpetwithgooglelogo) have 0 opacity." I just can't figure out how to say that in code.
The code as of right now look like this:
// Assign 4 booleans for the 4 arrow keys
var keyUp = false;
var keyDown = false;
var keyLeft = false;
var keyRight = false;
// Add the keyboard event (KEY_DOWN) on the stage
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey);
function pressKey(pEvent)
{
// If an arrow key is down, switch the value to true to the assigned variable
if (pEvent.keyCode == 38)
{
keyUp = true;
}
else if (pEvent.keyCode == 40)
{
keyDown = true;
}
else if (pEvent.keyCode == 37)
{
keyLeft = true;
}
else if (pEvent.keyCode == 39)
{
keyRight = true;
}
}
// Add the keyboard event (KEY_UP) on the stage
stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey);
function releaseKey(pEvent)
{
// If the arrow key is up, switch the value to false to the assigned variable
if (pEvent.keyCode == 38)
{
keyUp = false;
}
else if (pEvent.keyCode == 40)
{
keyDown = false;
}
else if (pEvent.keyCode == 37)
{
keyLeft = false;
}
else if (pEvent.keyCode == 39)
{
keyRight = false;
}
}
// Set the velocity of the object
var speed = 4;
// And the rotation speed
var rotationSpeed = 6;
// Add an enter frame event on the moving object
myCircle.addEventListener(Event.ENTER_FRAME, circleEnterFrame);
function circleEnterFrame(pEvent)
{
// Set the default velocity to 0 if no key is pressed
var velocity = 0;
if (keyUp)
{
// If the key up is pressed set the new velocity to the speed value
velocity = speed;
}
if (keyDown)
{
// If the key down is pressed set the new velocity to the half speed value
velocity = -speed/2;
}
if (keyLeft)
{
// rotate the object
pEvent.currentTarget.rotation -= rotationSpeed;
}
if (keyRight)
{
// rotate the object
pEvent.currentTarget.rotation += rotationSpeed;
}
// Convert the degreeAngle to the radian angle
var angleRadian = pEvent.currentTarget.rotation / 180 * Math.PI;
// Move the object with the radian angle and the object speed
pEvent.currentTarget.x += Math.cos(angleRadian) * velocity;
pEvent.currentTarget.y += Math.sin(angleRadian) * velocity;
}
Any help would be very much appreciated! Thanks!
The easiest way is to do something like this:
if( roomba.hitTestObject(google)){
google.alpha = 0;
}
You'll have to check this on every frame using an ENTER_FRAME event.
If this is confusing tell me in the comments and I will clarify with more code.

AS3: How do I play a sound when the currentframe ==?

My code below unfortunately results in the sound repeating at the framerate of the movie. I just need a simple "play a sound at a specific frame" function but can't figure this one out. Thanks for your help.
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2)
{
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
}
}
This seems about right. Are you sure the naturepage.ocean_btns movieclip is not stopped at the second frame or looping very few frames over and over?
Another option is to use a flag to see if you already played the sound:
var alreadyPlayedSound:Boolean = false;
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2 && !alreadyPlayedSound)
{
alreadyPlayedSound = true;
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
}
}
Or to remove the event listener once you played the sound:
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2)
{
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
removeEventListener(Event.ENTER_FRAME,soundFunction);
}
}