Skip to main content
Participant
February 25, 2016
質問

AS3 help needed if button1 of a movieclip && button2 of another movieclip clicked then gotoAndPlay

  • February 25, 2016
  • 返信数 1.
  • 430 ビュー

I made a memory cards game which has 24 card images from which the user has to find 7 images pf them in order to win. I am a begginer of actionscript 3 all the code is on frames instead of document or class files.


All 24 card images are movieclips. The set of 7 wining images have a button with the second state in the same layer with the win image. All the cards are created dynamically from the library and not in the stage.


My problem is what i ever have tried untill now for the last code in order to win had no success i'm very desperate please help me .....I cannot make two movieclips to communicate via variables.


Main timeline frame 6 has this code:


import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.*;
import flash.ui.Mouse;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.text.*;
import flash.display.Stage;

var objImage1:logo1 = new logo1;
var objImage2:pic2 = new pic2;
var objImage3:pic3 = new pic3;
var objImage4:logo2 = new logo2;
var objImage5:pic5  = new pic5;
var objImage6:pic6 = new pic6;
var objImage7:pic7 = new pic7;
var objImage8:logo3 = new logo3;
var objImage9:pic9 = new pic9;
var objImage10:pic10 = new pic10;
var objImage11:logo4 = new logo4;
var objImage12:pic12 = new pic12;
var objImage13:pic13 = new pic13;
var objImage14:pic14 = new pic14;
var objImage15:pic15 = new pic15;
var objImage16:logo5 = new logo5;
var objImage17:pic17 = new pic17;
var objImage18:logo6 = new logo6;
var objImage19:pic19 = new pic19;
var objImage20:pic20 = new pic20;
var objImage21:logo7 = new logo7;
var objImage22:pic22 = new pic22;
var objImage23:pic23 = new pic23;
var objImage24:pic24 = new pic24;
var sourceDeck:Array =[objImage1,objImage2,objImage3,objImage4,objImage5,objImage6,objImage7,objImage8,objImage9,objImage10,objImage11,objImage12,objImage13,objImage14,objImage15,objImage16,objImage17,objImage18,objImage19,objImage20,objImage21,objImage22,objImage23,objImage24];
var shuffledDeck:Array = new Array ();
var xValues:Array =["121","321","521","731","937","1136","120","320","520","732","938","1138","120","321","520","730","938","1135","121","322","520","732","938","1135"];
var yValues:Array =["173","173","173","173","173","173","346","346","346","346","346","346","534","534","534","534","534","534","716","716","716","716","716","716"];

function placeCards():void
{
for (var i:int = 0; i<sourceDeck.length; ++i)
{
  addChild
(sourceDeck[i]);
  sourceDeck
[i].x = int(xValues[i])+20;
  sourceDeck
[i].y = int(yValues[i]); 
}
}
placeCards
();
function removeCards():void
{
for (var i:int=0;  i<sourceDeck.length; ++i)

{
  removeChild
(sourceDeck[i]);

}
}
  
function shuffleCards():void
{
while(sourceDeck.length >0)
{
  
var r:int=Math.floor(Math.random()*sourceDeck.length);
  shuffledDeck
.push(sourceDeck[r]);
  sourceDeck
.splice(r,1);
}
}
function placeShuffledCards():void
{
for (var i:int=0; i<shuffledDeck.length; ++i)
{
  addChild
(shuffledDeck[i]);
  shuffledDeck
[i].x = int(xValues[i]);
  shuffledDeck
[i].y = int(yValues[i]);

}
}


function showShuffledCards():void

{  
removeCards
();
shuffleCards
();
placeShuffledCards
();

}
showShuffledCards
();
foo
();

function foo():void {
  
var x:int = 2; // you can use variables as you would normally

  
// do something here

  
var timer:Timer = new Timer(5000);
  
var afterWaiting:Function = function(event:TimerEvent😞void {
  timer
.removeEventListener(TimerEvent.TIMER, afterWaiting);
  timer
= null;
  
// 5 seconds have passed, now do something more


  objImage1
.gotoAndPlay(10);
  objImage2
.gotoAndPlay(10);
  objImage3
.gotoAndPlay(10);
  objImage4
.gotoAndPlay(10);
  objImage5
.gotoAndPlay(10);
  objImage6
.gotoAndPlay(10);
  objImage7
.gotoAndPlay(10);
  objImage8
.gotoAndPlay(10);
  objImage9
.gotoAndPlay(10);
  objImage10
.gotoAndPlay(10);
  objImage11
.gotoAndPlay(10);
  objImage12
.gotoAndPlay(10);
  objImage13
.gotoAndPlay(10);
  objImage14
.gotoAndPlay(10);
  objImage15
.gotoAndPlay(10);
  objImage16
.gotoAndPlay(10);
  objImage17
.gotoAndPlay(10);
  objImage18
.gotoAndPlay(10);
  objImage19
.gotoAndPlay(10);
  objImage20
.gotoAndPlay(10);
  objImage21
.gotoAndPlay(10);
  objImage22
.gotoAndPlay(10);
  objImage23
.gotoAndPlay(10);
  objImage24
.gotoAndPlay(10);
  
// the scope is retained and you can still refer to the variables you
  
// used earlier
  x
+= 2;
}
timer
.addEventListener(TimerEvent.TIMER, afterWaiting);
  timer
.start();

}


on each one of the seven buttons that each one is inside a movie clip I have this code:


import flash.display.Sprite;
import flash.text.*;
import flash.display.MovieClip;
import flash.events.MouseEvent;
var success:Boolean = false;
var butpic1:SimpleButton;
butpic1
.enabled = true;
addChild
(butpic1);

butpic1
.addEventListener(MouseEvent.CLICK, clickpics);

function clickpics(event:MouseEvent😞void {
gotoAndPlay
(1);
var myTextBox:TextField = new TextField();  
  myTextBox
.text = "Σωστό Συνέχισε!"; 
myTextBox
.border = true;  
  myTextBox
.borderColor = 0x000000;
myTextBox
.width = 180;  
myTextBox
.height = 87;  
myTextBox
.x = -77;  
myTextBox
.y = -126;
var myFormat:TextFormat = new TextFormat();
myFormat
.color = 000000;  
  myFormat
.size = 24;
myFormat
.align = TextFormatAlign.CENTER
myTextBox
.background = true;  
myTextBox
.backgroundColor = 0xFFF000;
myTextBox
.setTextFormat(myFormat);
  addChild
(myTextBox); 

/*var Success=true;*/
//MovieClip(parent).Success2();
//trace(event.target.name);
var success=true;
if(MovieClip(root).butpic3 && butpic1 == success)
  
{

  
MovieClip(root).gotoAndPlay("win");
  
}
//checking();
//trace(event.target);
//MovieClip(this.parent).success2();
//checking();
//MovieClip(root).check();
//trace("click: " + event.currentTarget.name);
}
//trace(MovieClip(root).but.butpic2);
/*function checking():void
{
if(butpic1 && MovieClip(root).butt.butpic3)
  {
  MovieClip(parent).gotoAndPlay("win");
  } else {
MovieClip(parent).gotoAndPlay("win");
}
}*/


/*function checking():void
{
if(MovieClip(root).Symbol1 && MovieClip(root).button2)
  {

  MovieClip(parent).gotoAndPlay("win");
  }
}*/


Can someone please help me??

このトピックへの返信は締め切られました。

返信数 1

kglad
Community Expert
Community Expert
February 25, 2016

it's hard to tell exactly what you're trying to do, but this will get you better organized:

var i:int;

var deck:Array = [];

var C:Class;

var card:MovieClip;

var cols = 8;

var gapX:int = 10;

var gapY:int = 10;

var numCorrectCards:int = 0;

createDeckF();

shuffle(deck);

placeCards();

function createDeckF():void{

for(i=1;i<=7;i++){

    C = Class(getDefinitionByName('logo'+i));

    card = new C()

    card.logo = true;

    deck.push(card);

    card.addEventListener(MouseEvent.CLICK,cardClickF);

}

var skipA:Array = [1,4,8,11,16,18,19,21];

for(i=2;i<25;i++){

    if(skipA.indexOf(i)==-1){

        C = Class(getDefinitionByName('pic'+i));

        card = new C();

        deck.push(card);

        card.addEventListener(MouseEvent.CLICK,cardClickF);

    }

}

}

function placeCards():void{

for (i = 0; i<deck.length; ++i){

  addChild(deck);

  deck.x = (i%cols)*deck.width+gapX;

  deck.y =  Math.floor(i/cols)*deck.height+gapY;

}

}

function removeCards():void{

for (i=0;  i<deck.length; ++i){

  removeChild(deck);

}

}

function revealCards():void{

    for (i=0;  i<deck.length; ++i){

        deck.gotoAndStop(10);

    }

}

function hideCards():void{

    for (i=0;  i<deck.length; ++i){

        deck.gotoAndStop(1);

    }

}

function cardClickF(e:MouseEvent):void{

    e.currentTarget.gotoAndStop(10);

    if(e.currentTarget.logo){

        numCorrectCards++;

        // this is a logo card, do something

       if(numCorrectCards==7){

           // all correct cards found.  do whatever

       }

    } else {

        //this is an incorrect card, do something else.

    }

}

function shuffle(a:Array) {

    var p:int;

    var t:*;

    var ivar:int;

    for (ivar = a.length-1; ivar>=0; ivar--) {

        p=Math.floor((ivar+1)*Math.random());

        t = a[ivar];

        a[ivar] = a

;

        a

= t;

    }

}

kglad
Community Expert
Community Expert
February 26, 2016

use:

var t:Timer = new Timer(7000,1);

var i:int;

var deck:Array = [];

var C:Class;

var card:MovieClip;

var cols = 8;

var gapX:int = 10;

var gapY:int = 10;

var numCorrectCards:int = 0;

createDeckF();

shuffle(deck);

placeCards();

revealCards();

t.addEventListener(TimerEvent.TIMER,hideCards);

t.start();

function createDeckF():void{

for(i=1;i<=7;i++){

    C = Class(getDefinitionByName('logo'+i));

    card = new C()

    card.logo = true;

    deck.push(card);

    card.addEventListener(MouseEvent.CLICK,cardClickF);

}

var skipA:Array = [1,4,8,11,16,18,19,21];

for(i=2;i<25;i++){

    if(skipA.indexOf(i)==-1){

        C = Class(getDefinitionByName('pic'+i));

        card = new C();

        deck.push(card);

        card.addEventListener(MouseEvent.CLICK,cardClickF);

    }

}

}

function placeCards():void{

for (i = 0; i<deck.length; ++i){

  addChild(deck);

  deck.x = (i%cols)*deck.width+gapX;

  deck.y =  Math.floor(i/cols)*deck.height+gapY;

}

}

function removeCards():void{

for (i=0;  i<deck.length; ++i){

  removeChild(deck);

}

}

function revealCards():void{

    for (i=0;  i<deck.length; ++i){

        deck.gotoAndStop(10);

    }

}

function hideCards(e:TimerEvent):void{

    for (i=0;  i<deck.length; ++i){

        deck.gotoAndStop(1);

    }

}

function cardClickF(e:MouseEvent):void{

    e.currentTarget.gotoAndStop(10);

    if(e.currentTarget.logo){

        numCorrectCards++;

        // show your yellow whatever

       if(numCorrectCards==7){

           // all correct cards found.  do whatever

       }

    } else {

        //this is an incorrect card, do something else.

    }

}

function shuffle(a:Array) {

    var p:int;

    var t:*;

    var ivar:int;

    for (ivar = a.length-1; ivar>=0; ivar--) {

        p=Math.floor((ivar+1)*Math.random());

        t = a[ivar];

        a[ivar] = a

;

        a

= t;

    }

}

mersini33作成者
Participant
February 26, 2016

Your code works well on the final step "4.the user has to find all 7 grey cards in order to win" but is not working on all the others for example

Initially the cards show the back side instead of the front and they are clickable from the start that I don't want this.....etc

I'm trying to separate from your code the function that I want for the last function "if user clicked all grey cards then wins" but no success.

In my code each movieclip has one layer from frame 1-9, which is the front side and from frame 10 is the back side that is a button and is clickable, also in frame 10  I have actionscript gotoAndPlay frame 1 --> that is the front side and I also create dynamically a textfield with the message

So I'm looking for a function that might be perhaps on each of the 7 movieclips on frame 10 whereas are functioning as buttons, I think I have to create an array with the wining cards and on click it will substract 1 I'm not sure if this will work... What do you think that you are more experienced.