Copy link to clipboard
Copied
Hello,
I made a memory game with a tutorial. My knowledge of AS3 is not that good so I have to following question.
There is a function like Math.Random to random generate number and put them on stage. I made a memory game and the cards are on the same place every time I play the "movie".
I want them to shuffle every time I restart the game. How can I write a good random function in AS3?
Thank you.
Joep van Dongen
1 Correct answer
I don't see any attempt in your code to try to randomize anything, but here's one approach I'll suggest.
Assign each card to an array as soon as each is instantiated, and assign the x and y coordinates of each one's location to another array (you can store the pairs as objects... { _x: x, _y: y ).
Once they are all created and the arrays are complete, shuffle one of those two arrays, or both if you like, and then go thru a loop and assign each instance a new location using the two arrays.
Copy link to clipboard
Copied
Can you show the code you have that relates to randomly arranging things that is not working? I cannot, and other will not, open your file.
Copy link to clipboard
Copied
Hello,
Cant you open it... hmm strange I will upload the .as file
Tnx for your quick response. I dont have a random function set yet.
I know I have to use a Random function but dont know how exactly. I think by using an array?
I will post the script files.
Copy link to clipboard
Copied
Maybe I got something,
Can I put all my Movieclips (cards) in a Array?
like this?
var array_shuffle:Array = new Array("Computer", "Muis", "Radio", "Iphone", "Beamer", "Platenspeler");
And then use this function to shuffle the array?
function array_shuffle($source:Array):Array {
var $return:Array = [];
while ($source.length > 0) {
$return.push($source.splice(Math.round(Math.random() * ($source.length - 1)), 1)[0]);
}
return $return;
}
}
I tested this but they keep comming at the same place...
Copy link to clipboard
Copied
Shuffling an array is what I would have recommended if I hadn't assumed you would be doing that by default. That function you show looks like it is some kind of PHP or Perl code. Below is a shuffling routine that is commonly handed out in these forums...
function shuffle(a:Array):Array {
var len:Number = a.length-1;
for (var ivar:Number = len; ivar>=0; ivar--) {
var p:Number = Math.floor(Math.random()*(ivar+1));
var t = a[ivar];
a[ivar] = a
;
a
= t;
}
return a;
}
So if you had your array you would shuffle by executing...
array_shuffle = shuffle(array_shuffle);
As for using strings in the array versus using instance references (no quotes) I probably would have gone with the instance references.
Copy link to clipboard
Copied
Hello,
Tnx for the reply I think I'm almost there but it still wont work.
This is what I got now.
code:
var array_shuffle:Array = new Array(Computer, Muis, Radio, Iphone, Beamer, Platenspeler);
function array_shuffle(a:Array):Array {
var len:Number = a.length-1;
for (var ivar:Number = len; ivar>=0; ivar--) {
var p:Number = Math.floor(Math.random()*(ivar+1));
var t = a[ivar];
a[ivar] = a
;
a
= t;
}
return a;
}
}
}
I think I'm going to give up on this one.
This is to difficult for me I think. Got this far but dont understand a think of doing this random thing with arrays.
Thank you for all the help!
Joep van Dongen
Copy link to clipboard
Copied
The problem may not be with the randomizing, but with the loading. You haven't shown that part of the code, so it's hard to say. As I said originally, some people can't and some people won't open files (either for security concerns, work policy, or because they don't care to have to sift thru files to find relevant things), so it is to your advantage to present your code in your posting and explain it.
Copy link to clipboard
Copied
So you still cant open my files?
I can post code:
Fla file = Memory.fla
DocumentClass = MemoryGame
This is MemoryGame.as :
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import Card;
import Computer;
import Muis;
import Radio;
import Iphone;
import Beamer;
import Platenspeler;
public class MemoryGame extends MovieClip
{
private var _card:Card;
private var _computer:Computer;
private var _muis:Muis;
private var _radio:Radio;
private var _iphone:Iphone;
private var _beamer:Beamer;
private var _platenspeler:Platenspeler;
private var _cardX:Number;
private var _cardY:Number;
private var _firstCard:*;
private var _totalMatches:Number;
private var _currentMatches:Number;
public function MemoryGame()
{
_totalMatches = 6;
_currentMatches = 0;
createCards();
}
private function createCards():void
{
_cardX = 45;
_cardY = 31;
for(var i:Number = 0; i < 2; i++)
{
_card = new Card();
addChild(_card);
_computer = new Computer();
_card.setType(_computer);
_card.x = _cardX;
_card.y = _cardY;
_cardX += _card.width + 50;
_card.addEventListener(MouseEvent.CLICK, checkCards);
}
for(var j:Number = 0; j < 2; j++)
{
_card = new Card();
addChild(_card);
_muis = new Muis();
_card.setType(_muis);
_card.x = _cardX;
_card.y = _cardY;
_cardX += _card.width + 50;
_card.addEventListener(MouseEvent.CLICK, checkCards);
}
_cardX = 45;
_cardY += _card.height + 50;
for(var k:Number = 0; k < 2; k++)
{
_card = new Card();
addChild(_card);
_radio = new Radio();
_card.setType(_radio);
_card.x = _cardX;
_card.y = _cardY;
_cardX += _card.width + 50;
_card.addEventListener(MouseEvent.CLICK, checkCards);
}
for(var l:Number = 0; l < 2; l++)
{
_card = new Card();
addChild(_card);
_iphone = new Iphone();
_card.setType(_iphone);
_card.x = _cardX;
_card.y = _cardY;
_cardX += _card.width + 50;
_card.addEventListener(MouseEvent.CLICK, checkCards);
}
_cardX = 45;
_cardY += _card.height + 50;
for(var m:Number = 0; m < 2; m++)
{
_card = new Card();
addChild(_card);
_beamer = new Beamer();
_card.setType(_beamer);
_card.x = _cardX;
_card.y = _cardY;
_cardX += _card.width + 50;
_card.addEventListener(MouseEvent.CLICK, checkCards);
}
for(var n:Number = 0; n < 2; n++)
{
_card = new Card();
addChild(_card);
_platenspeler = new Platenspeler();
_card.setType(_platenspeler);
_card.x = _cardX;
_card.y = _cardY;
_cardX += _card.width + 50;
_card.addEventListener(MouseEvent.CLICK, checkCards);
}
}
private function checkCards(event:MouseEvent):void
{
event.currentTarget.removeEventListener(MouseEvent.CLICK, checkCards);
if(_firstCard == undefined)
{
_firstCard = event.currentTarget;
}
else if(String(_firstCard._type) == String(event.currentTarget._type))
{
_firstCard = undefined;
_currentMatches ++;
if(_currentMatches >= _totalMatches)
{
}
}
else
{
_firstCard.gotoAndPlay("flipBack");
event.currentTarget.gotoAndPlay("flipBack");
_firstCard.addEventListener(MouseEvent.CLICK, checkCards);
event.currentTarget.addEventListener(MouseEvent.CLICK, checkCards);
_firstCard = undefined;
}
}
}
}
This is the Card.as:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Card extends MovieClip
{
public var _type:*;
public function Card()
{
this.buttonMode = true;
this.addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(event:MouseEvent):void
{
if(this.currentFrame == 1)
{
this.play();
}
}
public function setType(type:*):void
{
_type = type;
loader_mc.addChild(_type);
}
}
}
This is the how its put on screen:
So you can see its quiet easy to do this memory game 😉
I'm really desperate right now because I was really happy how far I got with this game:-)
But now I know my AS3 isnt that good at all.
Copy link to clipboard
Copied
I don't see any attempt in your code to try to randomize anything, but here's one approach I'll suggest.
Assign each card to an array as soon as each is instantiated, and assign the x and y coordinates of each one's location to another array (you can store the pairs as objects... { _x: x, _y: y ).
Once they are all created and the arrays are complete, shuffle one of those two arrays, or both if you like, and then go thru a loop and assign each instance a new location using the two arrays.
Copy link to clipboard
Copied
Hi Ned,
I finally got it
private var _cards:Array = new Array();
private function shuffleCards():void {
var tmpX:Number;
var tmpY:Number;
for each (var items in _cards) {
var n:int=Math.floor(Math.random()*_cards.length);
tmpX=_cards
tmpY=_cards
_cards
_cards
items.x=tmpX;
items.y=tmpY;
}
}
and for every card this:
_cards.push(_card);
And how sad it is I got my next problem...
I want to add a timer to my game.
The timer works fine but I want to add some if/else functions to it.
I want that when the timer is past the 0 you goto the nextframe and your gameover
And (maybe the hard part) I want when you have turned over all the cards you go to another frame, timer stops and left time is your score...
Where should I start?
Btw the timer code is on a actions keyframe on the timeline in the fla file.
This the code of the timer:
var timer:Timer = new Timer(100, 100);
timer.addEventListener(TimerEvent.TIMER, countdown);
timer.start();
function countdown(event:TimerEvent) {
myText.text = String(100 - timer.currentCount);
}
stop();
Thank you
Joepiooo
Copy link to clipboard
Copied
This isn't an answer as much as a direction. You described pretty well what you want to do, so look into your code tool kit and see which tools you can use to turn that description into functioning code. If you know you want to use a Timer, look it up and see what properties and methods and events support its use. What you have so far looks like a reasonable start for presenting the countdown.
Work at it, and if you hit a snag, start a new posting, explain your goal, and show the code you're trying to use.

