PDA

View Full Version : MC traced but not created



youwh
May 5th, 2008, 04:01 PM
I've duplicate a series of MC, but the thing is i can traced it and the result is its created with a random position, but it only shows 1 MC on stage, and the MC i traced, the 1st MC position is 0 by x,y,z where it should be random like the other MC.

And I can't seems to assign a buttonMode to it. It will somehow assign the buttonMode to the MCs and a Plane i created.

Whats wrong with my code here?



ActionScript Code:

public class pv3d extends Sprite {

private var viewport:Viewport3D;
private var scene:Scene3D;
private var camera:FreeCamera3D;
//private var camera:Camera3D;
private var renderer:BasicRenderEngine;

private var fc3d:FreeCamera3DController;

//----------------------------------------------------------------------------------------

private var p:Plane;

private var xml:XML;
private var xmlList:XMLList;
private var xmlLoader:URLLoader=new URLLoader;
private var pictureArray:Array=new Array;
private var textArray:Array=new Array;

//----------------------------------------------------------------------------------------

private var cube:Cube = new Cube(new MaterialsList({front:new MovieAssetMaterial("surFront",false,true),back:new MovieAssetMaterial("surBack",false,true),right:new MovieAssetMaterial("surRight",false,true),left:new MovieAssetMaterial("surLeft",false,true),top:new MovieAssetMaterial("surTop",false,true),bottom:new MovieAssetMaterial("surBottom",false,true)}), 40, 2, 42, 10, 10, 10);

//----------------------------------------------------------------------------------------

public function pv3d():void {


var base:MovieAssetMaterial=new MovieAssetMaterial("platform");
base.oneSide=false;
base.smooth=true;

//----------------------------------------------------------------------------------------

//primitive = new Plane(material applied to object, width, height, wSegments, hSegments);
p=new Plane(base,1000,750,3,3);
p.x=0;
p.y=0;
p.z=0;
scene.addChild(p);

//----------------------------------------------------------------------------------------

xmlLoader.load(new URLRequest("gallery/images.xml"));

xmlLoader.addEventListener(Event.COMPLETE,xmlLoade d);

//----------------------------------------------------------------------------------------


cube.addEventListener(MouseEvent.CLICK, objectPress );
//cannot assign directly to cube

//----------------------------------------------------------------------------------------
}

private function xmlLoaded(event:Event):void {
xml=XML(event.target.data);
xmlList=xml.children();



var picList:XMLList=xml.pic;//list for pic
cube.name="cube" + i;// set instance name

trace(cube);
cube.x=Math.random() * 960 - 460;
cube.y=Math.random() * 710 - 330;
cube.z=-40;
cube.rotationZ=Math.random() * 360;

scene.addChild(cube);
}
//addEventListener(MouseEvent.CLICK,mousedown);
}
//----------------------------------------------------------------------------------------

private function objectPress( e:MouseEvent ):void {
trace("mouse is click");
}
}
}



This is what I get from tracing the cube


cube0: x:0 y:0 z:0
cube1: x:490 y:14 z:-40
cube2: x:311 y:45 z:-40
cube3: x:-294 y:215 z:-40
cube4: x:24 y:-109 z:-40
If its traced, why its not showing on the stage?

skineh
May 5th, 2008, 04:21 PM
Hey youwh! I think the problem is that you defined cube outside of the class, so that it has scope everywhere. So, when you make a new cube, you're doing it using that variable. What that means is that rather than new cubes being created, you're just reassigning that single cube (which is why you can trace the properties of it each time you reassign it, but in the end, there's only one).

Also, is there a for loop in your xmlloaded handler? I don't see one, and looking at your code the way you have it posted, I don't see how on earth you're making more than one cube once your XML is loaded.

Anyways, assuming you just left out the beginning of your for loop (since I see an extra end bracket in there, that appears to be the case), then what you need to do is remove your cube declaration at the top, and do it inside the for loop in your xmlloaded handler instead. That way you're declaring new instances of cube each time, rather than reassigning your top-level declaration of cube.

EDIT - BTW, that also means you'll have to add your CLICK event listeners for the cubes inside the for loop as well, rather than in your pv3d constructor.

youwh
May 5th, 2008, 04:31 PM
Oops. I must have accidentally deleted the loop when cleaning up the code for easy viewing. :rabbit:

So this is what I have earlier

ActionScript Code:

for (var i:Number=0; i <picList.length(); i++) {
cube=new Cube(materials,40,5,42,10,10,10);
cube.name="cube" + i;// set instance name

trace(cube);
cube.x=Math.random() * 960 - 460;
cube.y=Math.random() * 710 - 330;
cube.z=-40;
cube.rotationZ=Math.random() * 360;

scene.addChild(cube);
addEventListener(InteractiveScene3DEvent.OBJECT_PR ESS, objectPress );
}



I have the cube in the loop, but if I trace the cube, all the position would show 0. So I'm not sure is that the best place to put in the cube.

And I cant assign the button to cube like cube.addEventLis.....
It just wont work. And If I just place it like what I did above, not only the cube is clickable, even the plane is :puzzle:

Hm... how do you actually trace a dynamic MC anyway? I tried
trace (MC1) and trace (MC[2]) but seems not the correct way. :puzzled:
And if i trace (MC) in the objectPress function, it only shows the data for the last MC

skineh
May 6th, 2008, 08:35 AM
Just to be sure - did you remove your cube declaration that you had before your constructor?

As far as tracing each cube goes (and accessing them later), I prefer pushing my instances onto an array, then using the array to access them. Declare the array before your constructor, so it has scope throughout the class (like you originally did with cube)...



private var cubeArray:Array = new Array();


Then when you're creating each cube, push them onto the array.




for (var i:Number=0; i <picList.length(); i++) {


cube=new Cube(materials,40,5,42,10,10,10);
cube.name="cube" + i;// set instance name


trace(cube);
cube.x=Math.random() * 960 - 460;
cube.y=Math.random() * 710 - 330;
cube.z=-40;
cube.rotationZ=Math.random() * 360;

cubeArray.push(cube); // add each instance to your array


scene.addChild(cube);
}


Now whenever you want to work with your cubes, you can do so via the array.



for(var i:uint = 0; i < cubeArray.length; i++)
{
trace(cubeArray[i]);
}


As far as your listeners go, maybe try assigning them to the array of instances?





for (var i:Number=0; i <picList.length(); i++) {


cube=new Cube(materials,40,5,42,10,10,10);
cube.name="cube" + i;// set instance name


trace(cube);
cube.x=Math.random() * 960 - 460;
cube.y=Math.random() * 710 - 330;
cube.z=-40;
cube.rotationZ=Math.random() * 360;

cubeArray.push(cube); // add each instance to your array


scene.addChild(cube);

cubeArray[cubeArray.length - 1].addEventListener(InteractiveScene3DEvent.OBJECT_P RESS, objectPress );
}


But really you should be able to just use "cube" there (cube.addEventListener). You sure that doesn't work for you if you do it inside that loop?

youwh
May 6th, 2008, 03:21 PM
Thanx skineh! so thats wat push for. :D
Somehow i mange to trace some weird data from one of the array. But I guess its ok.


undefined
undefined
undefined
cube3: x:-23 y:247 z:-5
cube3: x:-23 y:247 z:-5


cubeArray.addeventListener don't work either.
For the cube.addEventListener thing it doesn't work. I also don't know why.
Seems like it doesn't read "cube" cause i can't trace anything from the function.

Or can I trace what was clicked from the addEventLisener rather than hard-code it directly?