Presets with actionscript 3.0 - actionscript-3

This is the presets variables and to nav with multiple selections, the code below is not really correct and functions perfectly without errors, however it does not work.
var aClicked:Boolean = false;
var yyClicked:Boolean = false;
var tClicked:Boolean = false;
var oClicked:Boolean = false;
a.addEventListener(MouseEvent.CLICK, gotosomething1);
function gotosomething1(event:MouseEvent):void
{
gotoAndStop(89);
yyClicked = true;
activateT();
}
yy.addEventListener(MouseEvent.CLICK, gotosomething33);
function gotosomething33(event:MouseEvent):void
{
gotoAndStop(89);
tClicked = true;
activateT();
}
o.addEventListener(MouseEvent.CLICK, gotosomethinggg);
function gotosomethinggg(event:MouseEvent):void
{
gotoAndStop(89);
oClicked = true;
activateT();
}
t.addEventListener(MouseEvent.CLICK, gotosomething99);
function gotosomething99(event:MouseEvent):void
{
gotoAndStop(89);
aClicked = true;
activateT();
}
function activateT()
{
if (aClicked && yyClicked && oClicked)
{
t.addEventListener(MouseEvent.CLICK, gotosomething99);
}
}
yy.addEventListener(MouseEvent.CLICK, gogogo);
function gogogo(event:MouseEvent):void
{
gotoAndStop(89);
yyClicked = true;
activateYY();
}
t.addEventListener(MouseEvent.CLICK, gotosomethingplease);
function gotosomethingplease(event:MouseEvent):void
{
gotoAndStop(89);
tClicked = true;
activateYY();
}
o.addEventListener(MouseEvent.CLICK, gotoi);
function gotoi(event:MouseEvent):void
{
gotoAndStop(89);
oClicked = true;
activateYY();
}
a.addEventListener(MouseEvent.CLICK, millionare);
function millionare(event:MouseEvent):void
{
gotoAndStop(89);
aClicked = true;
activateYY();
}
function activateYY()
{
if (aClicked && tClicked && oClicked)
{
yy.addEventListener(MouseEvent.CLICK, gogogo);
}
}
t.addEventListener(MouseEvent.CLICK, gp1);
function gp1(event:MouseEvent):void
{
gotoAndStop(89);
tClicked = true;
activateA();
}
a.addEventListener(MouseEvent.CLICK, gp2);
function gp2(event:MouseEvent):void
{
gotoAndStop(89);
aClicked = true;
activateA();
}
o.addEventListener(MouseEvent.CLICK, gp3);
function gp3(event:MouseEvent):void
{
gotoAndStop(89);
oClicked = true;
activateA();
}
yy.addEventListener(MouseEvent.CLICK, gp4);
function gp4(event:MouseEvent):void
{
gotoAndStop(89);
yyClicked = true;
activateA();
}
function activateA()
{
if (yyClicked && tClicked && oClicked)
{
a.addEventListener(MouseEvent.CLICK, gp2);
}
}
o.addEventListener(MouseEvent.CLICK, ooo);
function ooo(event:MouseEvent):void
{
gotoAndStop(89);
oClicked = true;
activateO();
}
t.addEventListener(MouseEvent.CLICK, ttt);
function ttt(event:MouseEvent):void
{
gotoAndStop(89);
tClicked = true;
activateO();
}
a.addEventListener(MouseEvent.CLICK, aaa);
function aaa(event:MouseEvent):void
{
gotoAndStop(89);
aClicked = true;
activateO();
}
yy.addEventListener(MouseEvent.CLICK, yyy);
function yyy(event:MouseEvent):void
{
gotoAndStop(89);
yyClicked = true;
activateO();
}
function activateO()
{
if (yyClicked && tClicked && aClicked)
{
o.addEventListener(MouseEvent.CLICK, ooo);
}
}

This should work for what you are looking for:
import flash.events.MouseEvent;
import flash.events.Event;
var btnStates:Array = [false]; // Track states for each button - works w/ any # of btns
var btnCounter:int = 0; // Count each Active button
var btnActivateNumber = 4; // The number of buttons
this.addEventListener(MouseEvent.CLICK, btnClick); // Click on Anything Event
function btnClick(e:Event):void
{
var btnIndex = e.target.parent.getChildIndex(e.target); // Btn Index
trace("BtnIndex:", btnIndex); // Make Sure btns are are children of same parent
switch(e.target.name) // Check the name of item Clicked
{
case "a": // Checks that you clicked on button and not something else
case "yy": // Each of these cases could be done seperately
case "t": // Use descriptive names when possible
case "o": // Add additional buttons to track if needed
if(!btnStates[btnIndex]) // Btn not pressed before
{
btnCounter++; // Another Button Turned On
btnStates[btnIndex] = true; // Track Btn State
// Add Specific code related to button ie. Animation etc.
e.target.alpha = .75 // lighten Button
}
else // Use if you want the button to turn off on 2nd press
{
btnCounter--; // Button Turned Off
btnStates[btnIndex] = false; // Btn State Reset for this btn
}
break;
default:
trace("OtherItemClicked:", e.target.name); // Something else was clicked
// If you are getting wrong item when you click on your button
// You may have a child item in your button you will need to disable.
// set btnName.mouseChildren = false; or item.mouseEnabled = false;
break;
}
if(btnCounter == btnActivateNumber) // Test if enough buttons are pressed.
{
this.removeEventListener(MouseEvent.CLICK, btnClick); // CleanUp
trace(btnActivateNumber,"Buttons Activated");
// Place Your Navigation Action Code Here
}
}

Your question is a bit unclear. Do you want an action to happen when the 3rd button is clicked, or the fourth?
In any case, you can create a dictionary that has boolean values for each button. The key is the button, the value is set to true when that button is clicked. You can then count how many keys are positive in that dictionary to find out when "enough" have been clicked.

Related

Sprite movement basically right but

This is basically just drag and double click to set (so drag is temporarily disabled) but the sprite doesn't keep with the mouse- can someone point me to better code- otherwise I'll with go with this- so much more to do.
//The initial event performed when the button is first clicked;
internal var m_nDoubleClickSpeed:Number = 300;
internal var m_toMouse:Number;
internal var moveready:Boolean = false;
internal var initalx:uint;
internal var initialy:uint;
internal var move:Boolean;
internal function clickDoubleClick(e:MouseEvent):void {
if (isSet == false) {
this.startDrag();
}
if (isNaN(m_toMouse)==false) {
clearTimeout(m_toMouse);
HandleDoubleClick();
} else {
m_toMouse = setTimeout(HandleSingleClick, m_nDoubleClickSpeed);
}
}
internal function HandleSingleClick():void {
trace("HandleSingleClick");
trace("isSet");
trace(isSet);
this.stopDrag();
m_toMouse = NaN;
}
internal function HandleDoubleClick():void {
if (isSet == false) {
isSet = true;
this.stopDrag()
} else {
isSet = false;
}
trace("HandleDoubleClick");
m_toMouse = NaN;
}
internal function goodX(inX:Number):Number {
if (inX < 0) {
return 0;
}
if (inX > (stage.stageWidth) ) {
return (stage.stageWidth);
}
return inX;
}
internal function goodY(inY:Number):Number {
if (inY < 0) {
return 0;
}
if (inY > stage.stageHeight) {
return stage.stageHeight;
}
return inY;
}
I'm not sure if I understood you correctly. So you want to start drag on single click and drag until doubleclicked right? If so you can try something like that:
public class ClickAndDrag extends Sprite
{
private var clickTimeout:uint;
private var doubleClickSpeed:int = 500;
private var clickedOnce:Boolean;
private var mouseOnClick:Point;
private var isDragging:Boolean;
public function ClickAndDrag()
{
graphics.beginFill(Math.random()*0xffffff, 1);
graphics.drawCircle(0, 0, 20);
graphics.endFill();
this.buttonMode = true;
addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
}
private function handleMouseDown(e:MouseEvent):void
{
if (isDragging)
{
if (clickedOnce)
handleDoubleClicked();
else
{
//if it's not clicket within next doubleClickSpeed ms then doubleClickSpeed will be set to false;
clickedOnce = true;
clickTimeout = setTimeout( handleClickTimeout, doubleClickSpeed );
}
}
else
{
handleClickAndDrag();
}
}
//clicked once when dragging
private function handleClickOnce():void
{
graphics.clear();
graphics.beginFill(Math.random()*0xffffff, 1);
graphics.drawCircle(0, 0, 20);
graphics.endFill();
}
//clicked once when not dragging
private function handleClickAndDrag():void
{
isDragging = true;
this.addEventListener(Event.ENTER_FRAME, handleFrame);
mouseOnClick = new Point(this.mouseX, this.mouseY);
}
//doubleclicked when dragging
private function handleDoubleClicked():void
{
clearTimeout(clickTimeout);
clickedOnce = false;
this.removeEventListener(Event.ENTER_FRAME, handleFrame);
isDragging = false;
}
private function handleClickTimeout():void
{
clickedOnce = false;
handleClickOnce();
}
private function handleFrame(e:Event):void
{
this.x = stage.mouseX - mouseOnClick.x;
this.y = stage.mouseY - mouseOnClick.y;
}
}
It basically waits for mousedown and if it's already dragging it checks if you clicked once (changes color) ot twice (stops dragging). Or if it's not dragging yet then it starts dragging. You may also want to handle leaving the stage (Event.MOUSE_LEAVE).

How to activate and deactivate certain events? need help please.

I am trying to make a quizz. I am just starting to get into actionscript 3.0 so please any help would be appreciated.
So basically i am trying to make the four of the buttons that i have either activate or
deactivate their event listeners, i have tried "removeEventListener" and if statements however i cannot make it work. Please help me out, thanks.
/////////navigation for t,o,y and a////////////////////////////////
a.addEventListener(MouseEvent.CLICK, gotosomething1);
function gotosomething1 (event:MouseEvent):void
{
gotoAndStop(89);
}
yy.addEventListener(MouseEvent.CLICK, gotosomething2);
function gotosomething2 (event:MouseEvent):void
{
gotoAndStop(89);
}
t.addEventListener(MouseEvent.CLICK, gotosomething3);
function gotosomething3 (event:MouseEvent):void
{
gotoAndStop(89);
}
o.addEventListener(MouseEvent.CLICK, gotosomething4);
function gotosomething4 (event:MouseEvent):void
{
gotoAndStop(89);
}
/////me trying to use the if statement for removing event listener on "a"////////
if(MouseEvent.CLICK, gotosomething2) && (MouseEvent.CLICK, gotosomething3) && (MouseEvent.CLICK, gotosomething4)
{
a.removeEventListener(MouseEvent.CLICK, gotosomething1);
}
////////////////////end//////////////////////////////////////////
////////////////////////////////////////////////////
If you want to deactivate button a after button a is clicked, you would do this:
a.addEventListener(MouseEvent.CLICK, gotosomething1);
function gotosomething1 (event:MouseEvent):void
{
gotoAndStop(89);
a.removeEventListener(MouseEvent.CLICK, gotosomething1);
}
Edit:
If you want to activate button a after buttons yy, t and o have been clicked, you would need to keep track of their statuses using some extra variables.
var yyClicked:Boolean = false;
var tClicked:Boolean = false;
var oClicked:Boolean = false;
yy.addEventListener(MouseEvent.CLICK, gotosomething2);
function gotosomething2 (event:MouseEvent):void
{
gotoAndStop(89);
yyClicked = true;
activateA();
}
t.addEventListener(MouseEvent.CLICK, gotosomething3);
function gotosomething3 (event:MouseEvent):void
{
gotoAndStop(89);
tClicked = true;
activateA();
}
o.addEventListener(MouseEvent.CLICK, gotosomething4);
function gotosomething4 (event:MouseEvent):void
{
gotoAndStop(89);
oClicked = true;
activateA();
}
function activateA()
{
if(yyClicked && tClicked && oClicked)
{
a.addEventListener(MouseEvent.CLICK, gotosomething1);
}
}

removeEventListener not working

function Drag(event:MouseEvent):void {
if ((event.target.parent == InventoryMenu) && (event.target is item)) {
var picked:item = item(event.target);
stage.addEventListener(MouseEvent.MOUSE_UP, Drop);
InventoryArrowDown.addEventListener(MouseEvent.MOUSE_OVER, InventoryNav("down"));
InventoryArrowUp.addEventListener(MouseEvent.MOUSE_OVER, InventoryNav("up"));
function Drop(event:MouseEvent):void {
if ((event.target.parent == InventoryMenu) && (event.target is item)) {
var dropped:item = item(event.target);
if ((event.target is item) && (event.target.parent == InventoryMenu)) {
if (picked.itemdata("workswith") == dropped.name) {
var itemname:item = item(FetchResult(picked, dropped));
itemname.addChild(itemname.itemdata("filename"));
InventoryMenu.removeChild(picked);
InventoryMenu.removeChild(dropped);
InventoryMenu.addChild(itemname);
InventoryUpdate();
} else if (picked.name != dropped.name) {
trace("No son compatibles");
}
stage.removeEventListener(MouseEvent.MOUSE_UP, Drop);
InventoryArrowDown.removeEventListener(MouseEvent.MOUSE_OVER, InventoryNav("down"));
InventoryArrowUp.removeEventListener(MouseEvent.MOUSE_OVER, InventoryNav("up"));
}
}
}
}
}
For some reason the removeEventListener on InventoryArrowDown and InventoryArrowUp isn't working. I'm fairly sure the route is correct as it's a direct copy paste from the addEventListener and it uses no variables.
Any clue what's wrong?
Hard to help you without seeing the code of InventoryNav but maybe the issue is that you should remove the event listeners before your tests.
Also, you should write two different handlers instead of using one and passing an argument like you do.
Here is a modified version of your code that might help:
private function drag(event:MouseEvent):void {
if ((event.target.parent == inventoryMenu) && (event.target is Item)) {
var picked:Item = Item(event.target);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
inventoryArrowDown.addEventListener(MouseEvent.MOUSE_OVER, inventoryNavDown);
inventoryArrowUp.addEventListener(MouseEvent.MOUSE_OVER, inventoryNavUp);
}
}
private function drop(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP, drop);
inventoryArrowDown.removeEventListener(MouseEvent.MOUSE_OVER, inventoryNavDown);
inventoryArrowUp.removeEventListener(MouseEvent.MOUSE_OVER, inventoryNavUp);
if ((event.target.parent == inventoryMenu) && (event.target is Item)) {
var dropped:Item = Item(event.target);
if ((event.target is Item) && (event.target.parent == inventoryMenu)) {
if (picked.itemdata("workswith") == dropped.name) {
var itemname:Item = Item(fetchResult(picked, dropped));
itemname.addChild(itemname.itemdata("filename"));
inventoryMenu.removeChild(picked);
inventoryMenu.removeChild(dropped);
inventoryMenu.addChild(itemname);
inventoryUpdate();
} else if (picked.name != dropped.name) {
trace("No compatible sons");
}
}
}
}

How do I stop a hittestobject from going into another frame? FLASH AS3

I am creating a drag and drop game with a hittestobject, but when pressing the back button the label of the drag and drop game are still shown up on the back frame. Do i have to reset the game before pressing the back button?
Code:
right_mc.visible=false;
wrong_mc.visible=false;
var orig1X:Number=frog_mc.x;
var orig1Y:Number=frog_mc.y;
var orig2X:Number=queen_mc.x;
var orig2Y:Number=queen_mc.y;
var orig3X:Number=apple_mc.x;
var orig3Y:Number=apple_mc.y;
frog_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
frog_mc.addEventListener(MouseEvent.MOUSE_UP, frog_mcRelease);
queen_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
queen_mc.addEventListener(MouseEvent.MOUSE_UP, queen_mcRelease);
apple_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
apple_mc.addEventListener(MouseEvent.MOUSE_UP, apple_mcRelease);
done_btn.addEventListener(MouseEvent.CLICK, checkAnswers);
reset_btn.addEventListener(MouseEvent.CLICK, reset);
frog_mc.buttonMode=true;
queen_mc.buttonMode=true;
apple_mc.buttonMode=true;
function dragTheObject(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.startDrag();
var topPos:uint=this.numChildren-1;
this.setChildIndex(item, topPos);
}
function frog_mcRelease(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (drop_frog.hitTestPoint(item.x,item.y)) {
item.x=drop_frog.x;
item.y=drop_frog.y;
} else {
item.x=orig1X;
item.y=orig1Y;
}
};
function queen_mcRelease(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (drop_queen.hitTestPoint(item.x,item.y)) {
item.x=drop_queen.x;
item.y=drop_queen.y;
} else {
item.x=orig2X;
item.y=orig2Y;
}
};
function apple_mcRelease(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (drop_apple.hitTestPoint(item.x,item.y)) {
item.x=drop_apple.x;
item.y=drop_apple.y;
} else {
item.x=orig3X;
item.y=orig3Y;
}
};
function checkAnswers(event:MouseEvent):void {
if (drop_frog.hitTestPoint(frog_mc.x,frog_mc.y) &&
drop_queen.hitTestPoint(queen_mc.x,queen_mc.y) &&
drop_apple.hitTestPoint(apple_mc.x,apple_mc.y)) {
wrong_mc.visible = false;
right_mc.visible = true;
} else {
wrong_mc.visible = true;
right_mc.visible = false;
}
}
function reset(event:MouseEvent):void {
frog_mc.x=orig1X;
frog_mc.y=orig1Y;
queen_mc.x=orig2X;
queen_mc.y=orig2Y;
apple_mc.x=orig3X;
apple_mc.y=orig3Y;
right_mc.visible=false;
wrong_mc.visible=false;
}
Ideally you need to remove all eventListeners when you go back. And if you added some childs by addChild command of another one, you must remove chat childs. How did you added that "label of the drag and drop game" to screen?

to create quiz using StartDrag and stopDrag

Actually i have a quiz.fla. In the file ,two of them fill inthe blank questions and others are
multiple questions.
square1_mc must run only once not twice. İf user correct selected, doesnt run it again.
However,if mybadscoretext is 1 not increase 2,3,4. :S
how i can do all?
stop();
var myScore:Number = 0;
var myBadScore:Number=0;
square1_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(e:MouseEvent):void
{
e.target.startDrag();
}
function drop(e:MouseEvent):void
{
square1_mc.stopDrag();
if (square1_mc.hitTestObject(square2_mc)== true)
{
square1_mc.x=129;
square1_mc.y=133;
this.graphics.clear();
this.graphics.lineStyle(2, 000000);
this.graphics.moveTo(square1_mc.x, square1_mc.y);
this.graphics.lineTo(square2_mc.x, square2_mc.y);
this.graphics.endFill();
myScore += 1;
score_txt.text=String(myScore);
square1_mc.removeEventListener(MouseEvent.MOUSE_DOWN, drag);
}
else
{
square1_mc.x=129;
square1_mc.y=133;
myBadScore += 1;
mybadscore_txt.text=String(myBadScore);
}
}
Add a variable to keep track of whether the bad score has been added:
var badMarked:Boolean = false;
function drag(e:MouseEvent):void
{
e.target.startDrag();
badMarked = false;
}
[...]
function drop(e:MouseEvent):void
{
if (square1_mc.hitTestObject(square2_mc)== true)
{
[...]
}
else if ( !badMarked )
{
square1_mc.x=129;
square1_mc.y=133;
myBadScore += 1;
mybadscore_txt.text=String(myBadScore);
badMarked = true;
}
}