Search DLab 1.0

Wednesday, October 6, 2010

actionscript question

Aj Moe,
I have chosen to go with the Array method (once again, flash security is acting up), but I've run into a few difficulties. If you recall on the last presentation, I used Array with my AR to change the x,y position of the cubes randomly. That method, however, produced effect only when I restart flash player. This time, I want it so that the random effect take place every time the marker is detect (so that I do not have to restart flash quite so many time in the presentation) and I'm not sure how to produce that effect!

A bit lengthy but here's my code

package
{
import com.transmote.flar.FLARManager;
import com.transmote.flar.marker.FLARMarker;
import com.transmote.flar.marker.FLARMarkerEvent;
import com.transmote.flar.utils.geom.FLARPVGeomUtils;
import com.transmote.utils.time.FramerateDisplay;

import flash.display.Sprite;
import flash.events.Event;
import flash.media.Video;
import flash.net.URLLoader;
import flash.net.URLRequest;

import org.libspark.flartoolkit.support.pv3d.FLARCamera3D;
import org.osmf.image.ImageLoader;
import org.osmf.swf.SWFLoader;
import org.papervision3d.materials.BitmapFileMaterial;
import org.papervision3d.materials.MovieMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.render.LazyRenderEngine;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;



[SWF(width='640', height='480', backgroundColor='#000000', frameRate='40')]

public class midtermv2 extends Sprite
{
private var fm:FLARManager;
private var scene:Scene3D;
private var view:Viewport3D;
private var camera:FLARCamera3D;
private var lre:LazyRenderEngine;
private var p:Plane;
private var con:DisplayObject3D;
private var marker:FLARMarker;



public function midtermv2()
{
initFLAR();

}

private function initFLAR():void
{
fm = new FLARManager("flarConfig.xml");
fm.addEventListener(FLARMarkerEvent.MARKER_ADDED, onAdded);
fm.addEventListener(FLARMarkerEvent.MARKER_REMOVED, onRemoved);
fm.addEventListener(Event.INIT, init3D);
addChild(Sprite(fm.flarSource));
}

private function onAdded(e:FLARMarkerEvent):void
{
marker = e.marker;
p.visible = true;

}

private function onRemoved(e:FLARMarkerEvent):void
{
marker = null;
p.visible = false;

}

private function init3D(e:Event):void
{
scene = new Scene3D();
camera = new FLARCamera3D(fm.cameraParams);
camera.z = -30;
view = new Viewport3D(640, 480, true);
lre = new LazyRenderEngine(scene, camera, view);

var val:Array = ["pat.jpg", "pat2.jpg"]

var a:String = (val[int(Math.random() * val.length)]);

var mat:BitmapFileMaterial = new BitmapFileMaterial(a);

p = new Plane(mat, 320, 240);
p.scaleY = -1;
p.rotationZ = -90;
p.visible = false;

con = new DisplayObject3D();
con.addChild(p);

scene.addChild(con);
addChild(view);
addChild(new FramerateDisplay());

addEventListener(Event.ENTER_FRAME, loop);
}

private function loop(e:Event):void
{
if(marker != null)
{
con.transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(marker.transformMatrix);
}
lre.render();
}
}
}

The related lines of code is in the italic, and you can see what I did is set up an array, make a string variable call "a" which is equal to random string from the array, and then I parse "a" variable into the BitmapFileMaterial data (so AR will load that jpg)

You can see that they are all in the "init3D" function, and it work OK just like the last AR presentation with the cube. Now, however, I want the var a:String = (val[int(Math.random() * val.length)]); to take place in the "onAdded" function so that when the marker is detected, it select a random string from the array and then parse it to the
BitMapFileMaterial in the init3D function (so that I would get a random image every time marker is detected). I'm not quite sure how, becuase whenever I separate the var a:String = (val[int(Math.random() * val.length)]); and var mat:BitmapFileMaterial = new BitmapFileMaterial(a); into different function, the error comes up and say "a" is an undefined property.

Please help if you have the time, this'd been bugging me since yesterday. If you think its too much of a technical thing right now, I will leave it as is (restart flash every time to see random) and go work on production

PS. could it also be that init3D function needed to be nested inside the onAdded function for it to work. And sorry for being wordy on this post

1 comment:

  1. k, i'll have to look at this with you tomorrow,
    let's meet before GenEd at INDA office 12noon
    m

    ReplyDelete