ActionScript 3 insert image - actionscript-3

I need to insert image with original sizes into sprite with fixed width and height.
Then, I'd like to drag and drop this image.
My code for insert doesn't work.
public class Main extends Sprite{
public function Main() {
stage.align=StageAlign.TOP_LEFT;
var loader:Loader = new Loader();
loader.load(new URLRequest("image.jpg"));
var sprite:Sprite = new Sprite();
sprite.graphics.lineStyle(1);
sprite.graphics.beginFill(0xDDDDDD,0.5);
sprite.graphics.drawRect(0,0,400,300);
sprite.graphics.endFill();
sprite.addChild(loader);
addChild(sprite);
}
}
Thank!

will this work?
[Embed(source= "../lib/image.jpg")]
private var Image:Class;
var spriteData:BitmapData = BitmapAsset(new Image()).bitmapData;
var sprite:Sprite = new Sprite();
sprite.graphics.clear();
sprite.graphics.beginBitmapFill(spriteData);
sprite.graphics.drawRect(x, y, width, height);
sprite.graphics.endFill();
addChild(sprite);

Related

How to create a sprite from an image loaded in Loader()?

I'm loading a background image via the Loader() class and wanted to know if there's a way to create a sprite from that loaded image?
I'm wanting to put a function in an external class file to put the image in the loader and then call the class to create a sprite from the loaded image. I'm not even sure this is possible.
Note: I'm using flashdevelop and no timeline.
You can just use the loader object as a display object or you can access the Bitmap object in the loader and add that to a sprite.
var loader:Loader = new Loader();
loader.load(new URLRequest(filename));
addChild(loader);
loader.x = 100;
loader.y = 200;
//so on
To get access to the bitmap and bitmapdata loaded just add an event listener and access them.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
loader.load(new URLRequest(filename));
private function onLoadComplete(e:Event):void
{
var loaderInfo:LoaderInfo = e.target as LoaderInfo;
var loadedBitmap:Bitmap = loaderInfo.content as Bitmap;
var sprite:Sprite = new Sprite();
sprite.addChild(loadedBitmap);
addChild(sprite);
sprite.x = 100;
sprite.y = 200;
//so on
}

Is it possible to add a movieclip over a bitmap of StageWebView

I want to add a simple spinnerloader(movieclip) over a StageWebView html page for that i converted StageWebView page to bitmap but that didnt work please help me to solve this problem
var webView:StageWebView = new StageWebView();
var textGoogle:TextField=new TextField();
var textFacebook:TextField=new TextField()
textGoogle.htmlText="<b>Google</b>";
textGoogle.x=300;
textGoogle.y=-80;
addChild(textGoogle);
textFacebook.htmlText="<b>Facebook</b>";
textFacebook.x=0;
textFacebook.y=-80;
addChild(textFacebook);
textGoogles.addEventListener(MouseEvent.CLICK,goGoogle);
textFacebooks.addEventListener(MouseEvent.CLICK,goFaceBook);
webView.stage = this.stage;
webView.viewPort = new Rectangle(0, 100, stage.stageWidth, stage.stageHeight);
function goGoogle(e:Event):void
{
webView.loadURL("http://www.google.com");
webView.stage = null;
webView.addEventListener(Event.COMPLETE,handleLoad);
}
function goFaceBook(e:Event):void
{
webView.loadURL("http://www.facebook.com");
webView.stage = null;
webView.addEventListener(Event.COMPLETE,handleLoad);
}
function handleLoad(e:Event):void
{
var bitmapData:BitmapData = new BitmapData(webView.viewPort.width, webView.viewPort.height,true,0xffffff);
webView.drawViewPortToBitmapData(bitmapData);
var webViewBitmap:Bitmap=new Bitmap(bitmapData);
webViewBitmap.y = 100;
addChild(webViewBitmap);
}
This should be possible. Once you have created your bitmap:
1. remove the StageWebView from the stage
2. add your bitmap to the stage.
3. add your movie clip to the stage.

Mousechildren Issue

Consider the following code.
var sParent:Sprite = new Sprite();
var obj:Sprite = new Sprite();
var childA:Sprite = new Sprite();
var childB:Sprite = new Sprite();
sParent.addChild(obj);
obj.addChild(childA);
obj.addChild(childB);
childB.mouseChildren = false;
childB.mouseEnabled = false;
sParent.addEventListener(MouseEvent.CLICK, itemClickHandler);
sParent.addEventListener(MouseEvent.ROLL_OVER, onHoverIn);
sParent.addEventListener(MouseEvent.ROLL_OUT, onHoverOut);
Now, I want to detect events on "ChildA" but I do not want to detect children on "ChildB"
mouseChildren = false;
obviously isn't the solution in this particular case. Any ideas?
Looks like it is a limitation or a design feature of ROLL_OVER, check the following program I have changed the ROLL_OVER event to MOUSE_OVER and target and currentTarget are giving the correct sprites :
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var sParent:Sprite = new Sprite();
var obj:Sprite = new Sprite();
var childA:Sprite = new Sprite();
childA.graphics.beginFill(0xff0000);
childA.graphics.drawRect(0, 0, 100, 50);
childA.graphics.endFill();
var childB:Sprite = new Sprite();
childB.x = 150;
childB.graphics.beginFill(0x00ff00);
childB.graphics.drawRect(0, 0, 100, 50);
childB.graphics.endFill();
sParent.addChild(obj);
obj.addChild(childA);
obj.addChild(childB);
childB.mouseChildren = false;
childB.mouseEnabled = false;
sParent.mouseEnabled = false;
obj.mouseEnabled = false;
sParent.addEventListener(MouseEvent.CLICK, itemClickHandler);
sParent.addEventListener(MouseEvent.MOUSE_OVER, onHoverIn);
sParent.addEventListener(MouseEvent.MOUSE_OUT, onHoverOut);
addChild(sParent);
}
private function onHoverOut(e:MouseEvent):void
{
trace(e.currentTarget.name+ " "+e.target.name);
}
private function onHoverIn(e:MouseEvent):void
{
trace(e.currentTarget.name+ " "+e.target.name);
}
private function itemClickHandler(e:MouseEvent):void
{
trace(e.currentTarget.name+ " "+e.target.name);
}
}
}
I had the same problem and solved it with something really unexpectable...
var obj:Sprite = new Sprite();
var childA:Sprite = new Sprite();
var childB:Sprite = new Sprite();
obj.addChild(childA);
obj.addChild(childB);
obj.mouseEnabled = false; // this.
childB.mouseEnabled = false;
childB.mouseChildren = false;

How to beginBitmapFill without repeats?(AS3)

my code:
myCircle = new Shape();
function doStuffWithBitmapData(bmd:BitmapData):void
{
myCircle = new Shape();
var matrix:Matrix = new Matrix();
matrix.translate(0, 0);
myCircle.graphics.beginBitmapFill(bmd, matrix, false);
myCircle.graphics.drawCircle(0, 0, 17);
myCircle.graphics.endFill();
myCircle.x = 40;
myCircle.y = 63;
addChild(myCircle);
// your code
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event)
{
myCircle.rotation += 3;
}
I need to fill the circle with image , but the image is repeating many times, but if I set the repeat to false, the picture will be bigger, can I make no repeat and at the same time don't change the sizes of the filled image?
i'm not too sure of the the bitmapfill method, but creating your own bitmap and bitmapData, and then using your image's bitmapdata, you can manipulate the image pixel/data anyway you like.
-Using the setPixel/setPixel32 method of the bitmapData class will be helpful in your task (google is your friend)
Adobe's help: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html
Please look closely at the draw() method of BitmapData Class Documents. It is very important.
and refer a following code.
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Bitmap;
var myCircle:Shape;
var bmd:BitmapData = new BitmapData(600,400,false,0xffffff);
var bmp:Bitmap = new Bitmap(bmd);
this.addChild(bmp);
var circleBitmapData:BitmapData = new BitmapData(20,20,false,0xffffff * Math.random());
myCircle = new Shape();
var matrix:Matrix = new Matrix();
myCircle.graphics.beginBitmapFill(circleBitmapData);
myCircle.graphics.drawCircle(0, 0, 20);
myCircle.graphics.endFill();
myCircle.x = 40;
myCircle.y = 63;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(e:Event)
{
bmd.draw(myCircle, myCircle.transform.matrix, myCircle.transform.colorTransform);
myCircle.x = Math.random() * stage.width;
myCircle.y = Math.random() * stage.height;
}

positioning bitmapdata

I have the following code:
public function Application()
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
var urlRequest:URLRequest = new URLRequest("image/1.jpg");
loader.load(urlRequest);
addChild(loader);
}
private function completeHandler(e:Event):void{
loader.content.width = 800;
loader.content.scaleY = loader.content.scaleX;
piece = Math.round(loader.height/10);
drawBitmaps();
}
private function drawBitmaps():void{
var bmdata:BitmapData = new BitmapData(loader.width, piece, true, 0x000000);
bmdata.draw(loader);
var bitmap:Bitmap = new Bitmap(bmdata);
addChild(bitmap);
loader.visible = false;
}
the result is a bitmap wich contains a pice of the image. The height is 80. and it starts at the top of the image. But how can i tell the bitmapdata to start drawing the image from lets say 80pixels? so it draws a middle piece of the image? Because atm it allways draws from the top of the image.
You should use BitmapData::draw clipRect parameter.
Here is an example:
package {
import flash.geom.Rectangle;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Sprite;
public class BitmapDataTest extends Sprite {
public function BitmapDataTest() {
var c:Sprite = new Sprite();
var g:Graphics;
g = c.graphics;
g.beginFill(0xFF0000);
g.drawCircle(30,30,30);
g.endFill();
addChild(c);
c.x = 10;
c.y = 10;
var bmdata:BitmapData = new BitmapData(60, 60, true, 0x000000);
bmdata.draw(c,null,null,null, new Rectangle(0,30,30,30));
var bitmap:Bitmap = new Bitmap(bmdata);
addChild(bitmap);
bitmap.x = 80;
bitmap.y = 10;
}
}
}
Please notice that the target bitmap data should have the same dimensions as source or this won't work. If you really have to cut it down you should use BitmapData::copyPixels method.
The easiest way would be to apply a mask, dynamically. Here is a good example: Actionscript 3 and dynamic masks
You can create a rectangle, the height you're looking for and position it appropriately.
Hope this helps.