Player.IO Addchild not doing anything - actionscript-3

I am trying to add a simple rectangle to my stage.
Code:
private function makeBox()
{
trace("Test");
var myBox:Shape = new Shape();
myBox.graphics.beginFill(0xFF0000);
myBox.graphics.drawRect(50, 50, 150, 150);
myBox.graphics.endFill();
addChild(myBox);
}
But it doesnt do ANYTHING. All I see is a black stage (My stage color is set to black)
It doesnt work with a textfield either...
And yes, my playerio server is running. There are no errors in the console. This function is being run because the trace outputs to the console.

Related

AS3: Sprite does not dispatch mouse event

I am new to as3. I have developed a small application which in as3. But issue is the sprite created does not dispatch any mouse events [ eg: click ]. Please can anyone guide me and throw some comment
.... private var progressBarHolder:Sprite = new Sprite();
progressBarHolder.graphics.clear();
progressBarHolder.graphics.beginFill(0xeaeaea);
progressBarHolder.alpha = 0.5;
progressBarHolder.graphics.drawRoundRect(0, 0, 80, 25, 0,0);
//progressBarHolder.graphics.endFill();
progressBarHolder.width = progressBarWidth;
progressBarHolder.height = 24;
progressBarHolder.x = 48;
progressBarHolder.y = _videoModule.getHeight() - 48;
progressBarHolder.buttonMode = true;
progressBarHolder.addEventListener(MouseEvent.CLICK, progressBarHolderClick);
_overlay.addChild(progressBarHolder);
.....
Thanks!
Code looks ok, possible problems:
width and height of your sprite are not zero (progressBarWidth != 0)
mouse is enabled on parent (_overlay), to make sure use -
progressBarHolder.mouseEnabled = true;
make sure there is nothing above progressBarHolder on the Z index that is blocking the click from the sprite.
Your code is correct. Look up on _overlay or progressBarHolderClick listener. Also It's can happens if property mouseChildren of _overlay or other parent set as false.
Show more code if you still can't find solution.

In AS3, how can I check to see if mouse is inside a rectangle?

In AS3, how can I check to see if mouse is inside a rectangle after the stage has been clicked in that area?
for example:
var rec:Rectangle = new Rectangle(50, 200, 50, 200);
And the onclick function would do:
if (mouseX <=rec.left && etc...
I've tried many methods, but have still come up empty. Any ideas?
(What I really wanted to do was make an event listener for the rectangle, but it kept throwing errors so I'm trying to figure out a way to do it with an if statement instead.)
This isn't pertinent that I know this, because I can just do this with flat numbers. I'm trying to learn more about rectangles and points. I figured this would work with a rectangle too, but nope.
You can use Rectangles function contains which accepts 2 arguments x and y.
Like this:
var r:Rectangle = new Rectangle(50,200,50,200);
stage.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
if(r.contains(e.stageX,e.stageY))
{
trace("inside");
}
}

Drawing doesn't work

I'm playing with Actionscript and I can't get any of the drawing to work. I'm pretty much taking sample code from other websites and it doesn't seem to run. I'm working in Adobe Flash Professional CS6.
I currently have a (mostly) blank .fla document with a 800x500 white space to work with. So I go into the "timeline" tab, create a new keyframe, and under "actions", I have:
function onLoad():void
{
var lineDraw:Shape = new Shape();
lineDraw.graphics.lineStyle(3, 0x0000FF);
lineDraw.graphics.moveTo(100, 100);
lineDraw.graphics.lineTo(200, 200);
trace("test");
}
onLoad();
This prints the text "test" to the output window but does not draw the line.
What am I forgetting to do, or fail to understand?
You have to add the lineDraw shape to the stage. Otherwise, you are just drawing to a buffer.
function onLoad():void
{
var lineDraw:Shape = new Shape();
lineDraw.graphics.lineStyle(3, 0x0000FF);
lineDraw.graphics.moveTo(100, 100);
lineDraw.graphics.lineTo(200, 200);
addChild(lineDraw);
trace("test");
}
onLoad();
source: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Shape.html

How to set Video Frame Capture Using Bitmap Data

I'm implementing an Augmented Reality application for android using Flash. In order to get the application working on my Android Phone (nexus One) the Phone Camera must be activated as well. So I need 2 layers one for the background which is the feed of my phone camera and an other one on top of it which is the view from away3d in this case.
So setting up a BitmapData object to hold the information of the most recent webcam still-frame I can make this work.
If I use papervision3D library and FLARToolkit we setting up the BitmapData using the following part of the code found from this video tutorial:
//import libraries
import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
private function setupCamera():void
{
vid = new Video(640, 480);
cam = Camera.getCamera();
cam.setMode(320, 240, 10);
vid.attachCamera(cam);
addChild(vid);
}
private function setupBitmap():void
{
bmd = new BitmapData(640, 480);
bmd.draw(vid);
raster = new FLARRgbRaster_BitmapData(bmd);
detector = new FLARSingleMarkerDetector(fparams, mpattern, 80);
}
private function loop(e:Event):void
{
if(detector.detectMarkerLite(raster, 80) && detector.getConfidence() > 0.5)
{
vp.visible = true;
detector.getTransformMatrix(trans);
container.setTransformMatrix(trans);
bre.renderScene(scene, camera, vp);
}
else{
vp.visible = false}
}
catch(e:Error){}}}}
However, to implement my application Im using Away3D engine and FLARManager and the way of doing that is very different as I can understand. I have implement the following code but the only think it does is just show the Flash Camera in the front of the 3D view and I can't check if my application is work or not since it doesn't show me any 3D Object when I place the marker in front of the screen.
My code is:
//Setting Up Away3DLite Camera3D
import com.transmote.flar.camera.FLARCamera_Away3DLite;
private var camera3D:FLARCamera_Away3DLite;
this.camera3D = new FLARCamera_Away3DLite(this.flarManager, new Rectangle(0, 0, this.stage.stageWidth, this.stage.stageHeight));
//Setting Up the bitmapData
private function bitmap():void
{
c = Camera.getCamera();
c.setMode(320,240,10)
this.v.attachCamera(c);
addChild(this.v);
bmd = new BitmapData(640,480);
bmd.draw(this.v);
}
Can you please help me to find out how can I combine those two?
I will really appreciate any advice i can get from you.
Thank you
To isolate your problem, I'd try to break these two things up and make sure each part works first. It's sounds like you've got the camera part working, try doing just some 3D (no AR) draw a cube or something. Then try implementing the AR but have it do something simple like trace something out or making an object visible or invisible. Then start combining them.

drawRect in actionscript3

var blockGraphics : Graphics = null;
blockGraphics.clear();
blockGraphics.beginFill(255);
blockGraphics.drawRect(10,10,10,10);
I am trying to simply draw a rectangle but nothing appears on the screen. What am i missing?
Afaik you can't instantiate graphics class..
you need to make a MovieClip or Sprite and work with that.. you can't draw directly to stage.
var mc:MovieClip = new MovieClip();
mc.graphics.beginFill(0xFF0000);
mc.graphics.drawRect(10,10,10,10);
mc.graphics.endFill();
also don't forget to add it to stage
addChild(mc);
var mySprite:Sprite = new Sprite();
mySprite.graphics.beginFill(0x000000);
mySprite.graphics.drawRect(10, 10, 10, 10);
mySprite.graphics.endFill();
addChild(mySprite);
I don't really know a whole lot about the graphics class (I've used it a few times) but I don't believe you can call ANYTHING on a null object.
blockGraphics = null;
blockGraphics.clear();
is the same as calling:
null.clear();
Which is going to give you an error. Typically you'll want to take a movieclip or other such object and get it's graphics instance:
blockGraphics = mc.graphics;
blockGraphics.clear();
blockGraphics.beginFill(0xFF0000);
blockGraphics.drawRect(10,10,10,10);
would draw a red rectangle inside the "mc" movieclip.