Make an item disapear when the player touch 2 times the touch screen - actionscript-3

I'm trying to make a flash game for a touch screen (android). My player can take some items and put them in his inventory.
The player can, then, click on a item in his inventory and drag it where he wants on the scene.
But, I'd like to make the item disapear of the screen when the player double tap on the screen.
For the moment, my player need just to taps one. (and it's frustrating as, if he hasn't put the item into the correct space and wants to corrects it by dragging the item again, it's disappearing since he clicks on a wrong space).
I know that I have to change my "removeDraggedItem" function, but I can't figure it out how to tell it that I want this function only if the player ahs double tap on the screen (not just one click)
Here's my code. If someone has an idea....
public function itemClick(e:Event):void{
inv.draggingItem = false;
var nameofMC:String;
var tempMC;
var draggedName:String = draggedItem.displayName;
if (draggedItem.lookTag)
draggedName = draggedName + draggedItem.lookTag;
if (newFriend){
nameofMC = "action_"+draggedName+"_"+newFriend.displayName;
//trace ("Looking for "+nameofMC);
try {
tempMC = getDefinitionByName(nameofMC);
//trace ("MC found.");
removeDraggedItem();
if (speech)
speech.dispatchEvent(new Event("stopTalking"));
tempMC = new tempMC;
playerAction = new PlayerAction(stageRef, draggedItem, newFriend, tempMC, false);
}
catch(e){
//trace ("No MC found. Checking for dialog option...");
try {
var tempData = linesData.dialog[newFriend.displayName].useObject[draggedItem.displayName];
if (tempData != "" && tempData != null){
//trace ("Dialog option found.");
removeDraggedItem();
alignPlayer();
if (speech)
speech.dispatchEvent(new Event("stopTalking"));
dialog = new Dialog(stageRef, newFriend, draggedItem, false);
}
}
catch(e){
//trace ("No dialog option found. Defaulting to player line.");
alignPlayer();
if (speech)
speech.dispatchEvent(new Event("stopTalking"));
var actionName:String = "Use_"+newFriend.displayName;
if (newFriend.lookTag)
actionName = actionName+newFriend.lookTag;
speech = new Speech(stageRef, draggedItem, actionName);
}
}
} else {
removeDraggedItem();
}
}
private function removeDraggedItem():void{
stageRef.removeEventListener(MouseEvent.MOUSE_MOVE, dragItem);
stageRef.removeEventListener(Event.ENTER_FRAME, itemHitTest);
draggedItem.removeEventListener(MouseEvent.CLICK, itemClick);
stageRef.removeChild(draggedItem);
toolbar.useText.text = "";
if (stageRef.contains(this))
stageRef.removeChild(this);
Mouse.show();
Engine.playerControl = true;
}

On the first tap set a variable (eg screenTap) to true and set up a Timer for the maximum amount of milliseconds you will allow between taps in a double-tap. When the Timer expires reset screenTap to false.
Then, in your tap screen handler, check if screenTap is true. If so, remove the item.

Try to use MouseEvent.DOUBLE_CLICK instead of MouseEvent.CLICK.
Make sure double click is enabled for your stage :
stageRef.doubleClickEnabled=true;
then use :
stageRef.addEventListener(MouseEvent.DOUBLE_CLICK,removeDraggedItem);

Related

Making layers invisible with mouse click

Is it possible to make it so that when you click on a button the first time, a specific layer will become invisible... and then once you click on the button a second time, a different layer would become invisible, and so on? If so could I see an example? Thanks!
What I've tried :
/************************* RESET BUTTON **************************/
reset_btn.addEventListener(MouseEvent.CLICK,reset);
function reset(e:Event) : void
{
eraserClip.graphics.clear();
initEraser();
erasableBitmapData.fillRect(erasableBitmapData.rect, 0xFFFFFFFF);
penny.visible = true;
maskee4.visible = true;
card.visible = false;
greencard.visible = true;
}
The idea is, once I hit the reset button once, the layer named card, will disappear. Underneath that a layer will be there, which is titled greencard. Once I hit the reset button a second time I want the greencard to disappear. As you see above, I was just doing (property name).visible = false;. This works for the first card but not any after because they would not appear.
If I understand you correctly, you could try something like this below :
reset_btn.addEventListener(MouseEvent.CLICK, reset);
var clickCount : int = 0; //# start with zero since no clicks yet
card.visible = true;
greencard.visible = true;
function reset(e:Event) : void
{
clickCount += 1; //# adds +1 to current count of clicks
eraserClip.graphics.clear();
initEraser();
erasableBitmapData.fillRect(erasableBitmapData.rect, 0xFFFFFFFF);
penny.visible = maskee4.visible = true; //# if same value (true) you can chain them like this
if ( clickCount == 1) //if now 1 click
{
card.visible = false;
}
if ( clickCount == 2) //if now 2 clicks
{
greencard.visible = false;
}
}

Actionscript to make movie clip invisible before another is opened

I have a basic flash document that is showing football fixtures spread over several weeks. I have buttons for each week e.g. 'Gameweek 1', 'Gameweek 2' etc.
When the button is pressed, a movie clip is displayed below the buttons which shows the fixtures. The fixtures movie clip is always there but the button changes it to visible.
My problem is...if I have gameweek 1 fixtures showing and then i click on the 'Gameweek 2' button, both sets of fixtures are displayed because the gameweek 1 fixtures are still visible.
When I press a button to display new fixtures, I would like the previously visible movie clip to now be invisible so that just the new fixtures are visible.
Here is my actionscript:
stop();
btn_game1.addEventListener(MouseEvent.CLICK,openGame1);
function openGame1(evt:MouseEvent) {
if (game1.visible){
game1.visible = false;
}else {
game1.visible = true;
}
}
btn_game2.addEventListener(MouseEvent.CLICK,openGame2);
function openGame2(evt:MouseEvent) {
if (game2.visible){
game2.visible = false;
}else {
game2.visible = true;
}
}
I would like to propose a re-factored approach that cuts down on the amount of code and number of event handlers.
You only really need one button handler for all of your buttons. You'll also want to keep track of all of the clips that you're showing/hiding. They can be stored in an array.
You can "connect" a button to a clip by giving them similar names (btn_game1 and game1).
The following code assumes that your buttons are all named btn_gameN and your clips are named gameN (where N is a number):
var clips:Array = [game1, game2, game3, game4];
btn_game1.addEventListener(MouseEvent.CLICK, onGameButtonClicked);
btn_game2.addEventListener(MouseEvent.CLICK, onGameButtonClicked);
btn_game3.addEventListener(MouseEvent.CLICK, onGameButtonClicked);
btn_game4.addEventListener(MouseEvent.CLICK, onGameButtonClicked);
function onGameButtonClicked(e:MouseEvent):void
{
// figure out which button was just clicked by looking at its name
var clipNum:String = e.currentTarget.name.substr("btn_game".length);
// loop through all of your clips ...
for each(var clip:MovieClip in clips)
{
if(clip.name.substr("game".length) == clipNum)
{
// if the name matches, toggle the visibility
clip.visible = !clip.visible;
}
else
{
// if the name doesn't match, set visibility to false
clip.visible = false;
}
}
}
When you show game1 you must hide game2. Likewise for game2.
function openGame1(evt:MouseEvent) {
if (game1.visible) {
game1.visible = false;
}else {
game1.visible = true;
game2.visible = false;
}
}

Actionscript 3: How to detect when a button isn't pressed in time?

Okay, this is another question for the game I am making. I am putting together a second level, where you need to press a long series of keys at the right time. Pressing them too early or too late results in failure. What I need to know is how to detect when a key ISN'T pressed, when it should have been. This is what I have so far:
var count3:Number = 23;
var myTimer3:Timer = new Timer(1000, count3);
var timeLeft3:Number = count3;
var buttonPressed:Boolean = false;
var btnCounter:Number = 2;
var btnTimer:Timer = new Timer(1000, btnCounter);
myTimer3.addEventListener(TimerEvent.TIMER, countdown3);
myTimer3.start();
btnTimer.addEventListener(TimerEvent.TIMER, btnCountdown);
btn1.addEventListener(MouseEvent.CLICK, buttonPress);
btn1.visible = false;
btn2.visible = false;
btn3.visible = false;
btn4.visible = false;
btn5.visible = false;
btn6.visible = false;
btn7.visible = false;
btn8.visible = false;
btn9.visible = false;
function countdown3(event:TimerEvent): void {
if (((count3)-myTimer3.currentCount)==20) {
btn1.visible = true;
btnTimer.start();
} else if (((count3)-myTimer3.currentCount)==19) {
btn1.visible = true;
} else {
btn1.visible = false;
}
}
function btnCountdown(event:TimerEvent):void {
if (((btnCounter)-btnTimer.currentCount)==0) {
if (buttonPressed = true) {
btnTimer.stop();
} else {
gotoAndStop(2);
}
}
}
function buttonPress (event:MouseEvent): void {
buttonPressed = true;
}
For some reason, it won't do anything when btnCounter hits 0. If someone could help me sort this out, that would be awesome. Thanks.
N.B. This is a personal project, I am just learning actionscript
Start a timer for the time duration when the button is supposed to be pressed (for example, if the button must be pressed within 2 seconds, set the timer's interval to 2 seconds).
Then create a global Boolean variable (or class member variable, however your scene is set up) and set it to false initially. When the button is pressed, set this variable to true and disable the timer.
If the timer fires and this variable is false, it means that it hasn't been disabled by the button so the user didn't click the button within 2 seconds.
Don't use terribly short times - timers are not very accurate and processor speed may have an effect on their duration. (Human reaction times aside.)

What code in as-3 to use to allow seekbar to be moved by user?

I'm creating a simple music player which will play just one song. Stage timeline has got just one frame. There is main graphic for player which is movieClip_1 (it has 4 frames in it's own timeline) and that works ok. I've got button (button_2) which starts and pauses the song and the movieClip_1 (works ok). And i also have got a graphic (it's called tube - i have changed it to movie clip, it has got one frame inside it's timeline) as a seekbar component which I just want it to move correspondingly to channel.position of this song on the x axis which it does but gives me triple error of:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Debug points at this line:
tube.x = (chan.position/song.length) * 83;
I would really appreciate a tip regarding error and also what method to use in order for user to be able to navigate (with mouse) through song by moving tube on x axis and going to different part of song. I read about .startDrag and .stopDrag would that be good idea with my present code?
My code so far:
movieClip_1.stop();
var itsstoped:Boolean;
var youpausedit:Boolean;
var counter:Timer = new Timer(100);
var chan:SoundChannel;
var song:Sound = new Sound();
song.load(new URLRequest("wmc2.mp3"));
song.addEventListener(Event.COMPLETE, soundloaded);
function soundloaded(event:Event)
{
trace("Song has been loaded.");
}
counter.addEventListener(TimerEvent.TIMER,countcounter);
itsstoped = true;
youpausedit = false;
button_2.addEventListener(MouseEvent.CLICK, presser);
function presser(event:MouseEvent):void
{
if(itsstoped == true && youpausedit == true)
{
movieClip_1.gotoAndPlay(1);
chan = song.play(chan.position);
itsstoped = false;
}
else if(itsstoped == false)
{
movieClip_1.gotoAndStop(1);
chan.stop();
itsstoped = true;
youpausedit = true;
}
else if(itsstoped == true && youpausedit == false)
{
movieClip_1.gotoAndPlay(1);
chan = song.play();
counter.start();
itsstoped = false;
}
}
function countcounter(e:TimerEvent):void
{
trace(chan.position/song.length);
var percentage:uint = 100 * (chan.position / song.length);
trace(percentage);
if(percentage == 100)
{
movieClip_1.gotoAndStop(1);
itsstoped = true;
youpausedit = false;
counter.stop();
}
}
tube.buttonMode = true;
tube.useHandCursor = true;
addEventListener(Event.ENTER_FRAME, movewhenplay);
function movewhenplay(e:Event):void
{
tube.x = (chan.position/song.length) * 83;
}
Regarding the error, you're accessing an object that has yet to be instantiated, i.e. there is no data to fetch as it does not exist.
As for the searchbar, I'm guessing you're using some kind of mask for the search bar, as you're using its x position to display the song position. I would suggest you instead use its width parameter, and set the center point to the far left.
To be able to then search through the song, you can add a copy of this searchbar, place it on top of the first one. Set its alpha to 0, to make it invisible, but still clickable. Add mouseEvent listeners to that, and when the user clicks it, you can set the song to play from the position which you can calculate using mouseX relevant to the invisible search bar.
I.e. if you want the clicked position in percents
percent = (mouseX - invisiSearchBar.x) / invisiSearchbar.width

Resize the movie clip when change the frame

Please help, i have the following code ( Two frames, ones logined it goes to frame 2 and stop. ).
What i am after is to change the whole movie clip size to very small ones it's in frame2. please any advice is much appreciated.
stop();
stage.addEventListener(MouseEvent.RIGHT_CLICK, function(e:Event){});
login_btn.buttonMode = true; // show hand cursor
//adding event listener
login_btn.addEventListener(MouseEvent.CLICK, loginUser);
function loginUser(e:MouseEvent):void{
//if username & password are correct allow entry
if(IDTxt.text == "wixlab"){
gotoAndStop(2);
}
if(IDTxt.text == "hello"){
gotoAndStop(2);
}
if(IDTxt.text == "hello"){
gotoAndStop(2);
}else {
//otherwise show an error message
var alert:TextFormat = new TextFormat();
alert.color = 0xff0000; //red font
messageTxt.text = "Incorrect ID, Please try again.";
messageTxt.setTextFormat(alert);
}
}
If you want to get rid of the button, there are many ways to do it:
Get rid of the button permanently
removeChild(login_btn);
If you actually just want to shrink the button so you can't see it
login_btn.scaleX = 0;
If you want the button to still exist but not see it:
login_btn.x = -1000;