0

I've 3 mc. I want to call 2 of them on stage randomly in specific locations. I don't know how to call them. I just tried with array. I think array is the best way but still confused.

this's code I tried :

import flash.geom.Point;

var Batumc:batu_mc = new batu_mc(); // creates a instance of the movieclip, i.e, an object
var Batumc1:L = new L();
var Pisangmc:pisang_mc = new pisang_mc();

var Batumc2:MovieClip = new MovieClip();

var Status:int = 0;

button.addEventListener(MouseEvent.CLICK, tombol);
    function tombol(e:MouseEvent):void{
        //addChild(Batumc);
        //addChild(Batumc1);
        //addChild(Pisangmc);

        var P:Array = [new Point(80.2, 100), new Point(260, 100), new Point(430, 100)];
        var M:Array = [Batumc, Batumc1, Pisangmc];


        //random benda
        var benda:int = Math.random()*M.length;

        // Remove the selected benda from its list.
        M.splice(benda, 1);

        while (M.length){
        // Get the last MovieClip and remove it from the list.
        Batumc2 = M.pop();
        trace(Batumc2);
        // Produce a random Point.
        var anIndex:int = Math.random() * P.length;
        var aPo:Point = P[anIndex];


        // Remove the selected Point from its list.
        P.splice(anIndex, 1);

        // Move the selected MovieClip to the selected Point coordinates.
        Batumc2.x = aPo.x;
        Batumc2.y = aPo.y;


        addChild(Batumc);
        addChild(Batumc1);
        addChild(Pisangmc);

        }
        Status = 1;
    }

button.addEventListener(Event.ENTER_FRAME, frame);
function frame(e:Event):void{
    if(Status == 1 ){
        removeChild(Batumc2);
        Status = 0;
    }
}

when i run this code, sometimes 3 mc appear again

Tamara
  • 23
  • 7

4 Answers4

0

I think the problem is with this line:

M[i] = P.splice(randomPos, 1);

You assign M[i] (array of mc) with value from P (array of points), so M[i] = array with point and not an mc.

If you want to avoid such issues you can use Vectors instead of arrays. Its more efficient and also must contain the type you declared. Vector class Docs.

To use the vector, instead of:

var P:Array = [...];
var M:Array = [...];

Do:

var P:Vector.<Point> = new <Number>[...];
var M:Vector.<MovieClip> = new <MovieClip>[...];

And the rest is just like arrays (pop, push, slice, ...)

To remove item from M, just splice M, i don't understand why you need to iterate over M.

M.splice(randomPos, 1);
Lior Hai
  • 477
  • 2
  • 5
  • i've seen it but if I want to use it, the question same how to use it? @Lior Hai – Tamara Sep 26 '19 at 07:25
  • I edit the answer, but i don't understand what you are trying to do with this for loop. are you trying to change M so it will have 2 items instead of 3 (remove 1 in random)? – Lior Hai Sep 27 '19 at 05:35
  • I've 3 items on M (mc) and 3 items on P(point). I want to random them. when I call them, just 2 items on M show up on stage based on 2 items in P. So, just 2 items on M and P were called and show up on stage. it's difficult to explain but I hope you understand what I mean @Lior Hai Ps: I edited my code – Tamara Sep 27 '19 at 08:48
  • I dont understand why you add all MCs in the while loop: addChild(Batumc); addChild(Batumc1); addChild(Pisangmc); Just add the one you poped from M: addChild(Batumc2) – Lior Hai Oct 02 '19 at 05:38
  • I've tried before. Sometimes it works, sometimes not. and when I use ````removechild```` I got error @Lior Ha – Tamara Oct 02 '19 at 06:52
  • 1
    You get the error because Batumc2 is assigned value twice, once for each iteration, when. The second assignment overrides the first. You actually do: Batumc2 = x; Batumc2 = y; and its assigned with the value of y eventually. And what do you mean by sometimes it works? – Lior Hai Oct 02 '19 at 10:33
  • "And what do you mean by sometimes it works? ", I've been checked and that's my fault, I put the wrong code. But can you explain to me again with example? @Lior Hai – Tamara Oct 03 '19 at 02:32
  • Look, i am sorry but i don't understand much of your intentions in the code. Why do you remove the MC in 'enter_frame' listener? You know its being called on every frame? It does not look very efficient. Anyway, you can remove child after after checking if its in the display: if (contains(Batum)) removeChild(Batum); – Lior Hai Oct 03 '19 at 05:24
  • I know it's being called in every frame. I remove the mc coz when i want to go to the next scene all of mc must remove. if I dont remove the mc, I'll get error. @Lior Hai – Tamara Oct 03 '19 at 07:23
0

Try getDefinitionByName

var myClass:Class = getDefinitionByName("Class_Name") as Class;
var classInstance:MovieClip = new myClass as MovieClip; 
addChild(classInstance);
0

You just have to determine the specific location in the array for the mc'c, beside that you can get the classes from the library like this;

var locations:Array = [{xpos:100, ypos:12} , {xpos:30, ypos:50} , {xpos:400, ypos:28}, ......];

for(var i:uint=0; i<YOUR_MCS_LENGTH; i++) {
      var myClass:Class = getDefinitionByName("Batumc"+i) as Class;
      var mc:MovieClip = new myClass as MovieClip;
      mc.x = locations[i].xpos;
      mc.y = locations[i].ypos;
}
0

I have followed your advice @Özgün Sandal, but I get this error

ReferenceError: Error #1065: Variable Batumc10 is not defined. at global/flash.utils::getDefinitionByName() at nyoba9_fla::MainTimeline/frame1()

this my code :


    import flash.utils.getDefinitionByName;
    import flash.geom.Point;

    var Batumc:batu_mc = new batu_mc(); // creates a instance of the movieclip, i.e, an object
    var Batumc1:L = new L();
    var Pisangmc:pisang_mc = new pisang_mc();

    var P:Array = [new Point(80.2, 100), new Point(260, 100), new Point(430, 100)];
    var M:Array = [Batumc, Batumc1, Pisangmc];

for(var i:uint=0; i<M.length; i++) {
      var myClass:Class = getDefinitionByName("Batumc1"+i) as Class;
      var mc:MovieClip = new myClass as MovieClip;

      // Produce a random Point.
        var anIndex:int = Math.random() * P.length;
        var aPo:Point = P[anIndex];


        // Remove the selected Point from its list.
        P.splice(anIndex, 1);

      mc.x = aPo.x;
      mc.y = aPo.y;
}

where's my fault?

Tamara
  • 23
  • 7