Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Why some pictures disappear in AS3?

Guest
Oct 27, 2015 Oct 27, 2015

There are 17 symbols my project. Each symbol contains one image. It is required display 9 random images and shuffle them. symbols are named: p1 , p2 , p3 and so on ... The project works properly except that one or two images disappears from the 9 random images!!

I can not find the reason?? Would you help me, please? Thanks in advance,

This images features 3 runs (CTRL + Enter) of my project.

tests.jpg

Here is my code:

stop();

var  i:int; i=0;

var pic:Array =[9];

var c :int;

do

{

  pic=randomRange(1, 17);

 

  for (c=1;c<i;c++) // the "for loop" prevents repeating pictures

  {

   if (pic==pic[i-c])

  

    {

     pic=randomRange(1, 17);

  trace("repeated pic:" , c)

  c--;

    }

  }

 

// re-arranging some picturs using nine "if" conditions:

if (i==0)

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 100 ;

this ["p"+pic].y = 75 ;

trace (i);

}

  if (i==1)

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 280 ;

this ["p"+pic].y = 75 ;

  trace (i);

}

  if (i==2)

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 460 ;

this ["p"+pic].y = 75 ;

  trace (i);

}

if (i==3) // new row

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 100;

this ["p"+pic].y = 255;

  trace (i);

}

  if (i==4)

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 280;

this ["p"+pic].y = 255;

  trace (i);

}

   if (i==5)

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 460;

this ["p"+pic].y = 255;

  trace (i);

}

  if (i==6) // new row

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 100;

this ["p"+pic].y = 435;

  trace (i);

}

  if (i==7)

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 280;

this ["p"+pic].y = 435;

  trace (i);

}

   if (i==8)

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 460;

this ["p"+pic].y = 435;

  trace (i);

}

trace (pic) ;

i++;

}

while (i<9)

function randomRange(minNum:Number, maxNum:Number):Number // This function produces a random number

{

    return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);

}

TOPICS
ActionScript
395
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Oct 28, 2015 Oct 28, 2015

Here is another way to arrange the symbols:

var column:Number = 3;

var i:uint;

var list:Array = [];

var pic:Array = [];

stop();

for(var p:uint=1; p<=17; p++)

{

    list.push(this.getChildByName("p" + p))

}

shuffle(list);

pic = list.splice(0,9);

for(i = 0; i < pic.length; i++ ) // Arrange the symbols

{

    pic.width = 175;

    pic.height = 175;

    pic.x = 100 + Math.floor( i % column ) * 185; // the pic width + 10 as space

    pic.y = 100 + Math.floor( i / column  ) * 185;

}

function shuffle

...
Translate
Enthusiast ,
Oct 27, 2015 Oct 27, 2015

The disappeared pictures are the repeated pictures

if (pic == pic[i - c])

        {

            pic = randomRange(1, 15);

            trace("repeated pic:", c)

           c--; // this line is removing 2 items from the array which have the same name (pic[i - c])

        }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 27, 2015 Oct 27, 2015

Hi nezarov, It is required not to repeat the pictures that appear on the screen. So, I check if there are two symbols that has the same name (The same picture). If it is true, the duplicated picture will get a new name. And then I will perform a re-check using C-- line. I've commented out the whole "for loop" but the problem still exits !! So, I don't think that C-- line is removing any pictures. Would you demonstrate your point of view, Please? I would be thankful if you wrote the right code and Thank you very much for your effort. if (pic == pic[i - c]) // Check if there is a duplicated symbols (pictures)         {             pic = randomRange(1, 15); /*Replace the duplicated pictures with a new name */             trace("repeated pic:", c)           c--; /* This line is to re-check or make sure that the new name is not duplicated with any other symbols in the array pic */         }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 27, 2015 Oct 27, 2015

sorry, I've put the codes in a way that is hard to read: if (pic == pic[i - c]) // Check if there is a duplicated symbols (pictures)         {            pic = randomRange(1, 15); /*Replace the duplicated pictures with a new name */             trace("repeated pic:", c)          c--; /* This line is to re-check or make sure that the new name is not duplicated with any other symbols in the array pic */        }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 27, 2015 Oct 27, 2015

this code doesn't giving a new name it's just moving the symbol again

if (pic == pic[i - c])

        {

            pic = randomRange(1, 15);

            trace("repeated pic:", c)

            c--;

        }

what you need is to find a way to generate a random numbers without repeating

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 27, 2015 Oct 27, 2015
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 27, 2015 Oct 27, 2015

This is working good

stop();

var list:Array = new Array;

for(var p:uint=1; p<=17; p++)

{

    list.push(p);

}

shuffle(list);

trace(list);

   

var pic:Array = list.splice(0,9);

trace(pic);

for(var i:uint=0; i<=pic.length; i++)

{

    if (i == 0)

    {

        this["p" + pic].width = 175;

        this["p" + pic].height = 175;

        this["p" + pic].x = 100;

        this["p" + pic].y = 75;

       

        trace(i);

    }

    if (i == 1)

    {

        this["p" + pic].width = 175;

        this["p" + pic].height = 175;

        this["p" + pic].x = 280;

        this["p" + pic].y = 75;

       

        trace(i);

    }

    if (i == 2)

    {

        this["p" + pic].width = 175;

        this["p" + pic].height = 175;

        this["p" + pic].x = 460;

        this["p" + pic].y = 75;

       

        trace(i);

    }

    if (i == 3) // new row

    {

        this["p" + pic].width = 175;

        this["p" + pic].height = 175;

        this["p" + pic].x = 100;

        this["p" + pic].y = 255;

       

        trace(i);

    }

    if (i == 4)

    {

        this["p" + pic].width = 175;

        this["p" + pic].height = 175;

        this["p" + pic].x = 280;

        this["p" + pic].y = 255;

       

        trace(i);

    }

    if (i == 5)

    {

        this["p" + pic].width = 175;

        this["p" + pic].height = 175;

        this["p" + pic].x = 460;

        this["p" + pic].y = 255;

       

        trace(i);

    }

    if (i == 6) // new row

    {

        this["p" + pic].width = 175;

        this["p" + pic].height = 175;

        this["p" + pic].x = 100;

        this["p" + pic].y = 435;

        trace(i);

    }

    if (i == 7)

    {

        this["p" + pic].width = 175;

        this["p" + pic].height = 175;

        this["p" + pic].x = 280;

        this["p" + pic].y = 435;

        trace(i);

    }

    if (i == 8)

    {

        this["p" + pic].width = 175;

        this["p" + pic].height = 175;

        this["p" + pic].x = 460;

        this["p" + pic].y = 435;

        trace(i);

    }

}

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;

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 28, 2015 Oct 28, 2015
LATEST

Here is another way to arrange the symbols:

var column:Number = 3;

var i:uint;

var list:Array = [];

var pic:Array = [];

stop();

for(var p:uint=1; p<=17; p++)

{

    list.push(this.getChildByName("p" + p))

}

shuffle(list);

pic = list.splice(0,9);

for(i = 0; i < pic.length; i++ ) // Arrange the symbols

{

    pic.width = 175;

    pic.height = 175;

    pic.x = 100 + Math.floor( i % column ) * 185; // the pic width + 10 as space

    pic.y = 100 + Math.floor( i / column  ) * 185;

}

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;

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines