beginBitmapFill() is not working for existing movieclip, help me - actionscript-3

I want to fill the image to the movie clip using beginBitmapFill(), but I don't see the image. Actually, I have created a movie clip with box and I skewed it. I want to show the image inside the skewed box. And Image also should look skewed (needs to fill inside the box)
Here is my action script code :
package{
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.LoaderInfo;
public class AttachMovieClipExample extends Sprite{
public var rect:MovieClip;
public function AttachMovieClipExample()
{
rect = new redRectangle();
var bitmapData:BitmapData;
var loader:Loader = new Loader();
loader.load(new URLRequest("sam.jpg"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
function onComplete (event:Event):void
{
bitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;
rect.graphics.clear();
rect.graphics.beginBitmapFill(bitmapData,null,false,true);
// rect.graphics.endFill();
rect.x = 400;
rect.y = 210;
addChild(rect);
}
}
}
}

In your onComplete method you are missing the call to drawRect
function onComplete (event:Event):void
{
bitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;
rect.graphics.clear();
rect.graphics.beginBitmapFill(bitmapData,null,false,true);
rect.graphics.drawRect(x,y,height,width) //where x, y, height and width are values
rect.graphics.endFill();
rect.x = 400;
rect.y = 210;
addChild(rect);
}
The docs for the graphics class give you more information.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Graphics.html#drawRect()

Try this: 1. beginBitmapFill() 2. drawRect() 3. endFill() (never forget step 3!)

Related

Move projectile to mouse-click position AS3

I have a cannon and cannonball. How do I make a cannonball to move in a line from cannon to the Mouse Click position and stop/disappear/activate Explode animation?
I've tried different solutions and none of them would seem to work for me so I cleared it a bit.
And yes, I know it's ugly.
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Mouse;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.utils.Timer;
import flash.display.Sprite;
addEventListener(Event.ENTER_FRAME, enterFr);
function enterFr(e:Event)
{
aims.x = mouseX;
aims.y = mouseY;
}
Mouse.hide();
zamok.addEventListener(MouseEvent.CLICK, fire);
function fire(m:MouseEvent)
{
var s:Sound = new cannonFire();
s.play();
var explo:boom = new boom();
explo.x = mouseX;
explo.y = mouseY;
addChild(explo);
}
Well you should think of the process that you want to implement.
First of all it is not instant, it takes some time for cannonball to move to the point where mouse clicked.
Let's start with some function that will create a cannonball:
private function fireCannonBall(target: Point):void
{
const cannonBall:CannonBall = new CannonBall(); // you need to implement this class, or just use some MovieClip from library;
cannonBall.x = initialPosition.x; // initial position is a point where your cannon is located.
cannonBall.y = initialPosition.y;
addChild(cannonBall);
// I suggest using TweenNano, but it has some limitations, read the License Agreement carefully
TweenNano.to(cannonBall, 15 /* animation duration */, {x: target.x, y: target.y, onComplete: makeExplosion, onCompleteParams: [cannonBall]});
}
private function makeExplosion(cannonBall: CannonBall):void
{
/* I leave this part to you, here you might want to launch some explosion animation */
}
Now we need to handle the click:
private function onMouseClick(e: MouseEvent):void
{
const target: Point = new Point(stage.mouseX, stage.mouseY);
//and launch the cannonBall:
fireCannonBall(target);
}
That's it, roughly.
To know more about TweenNano please follow the link:
https://greensock.com/tweennano-as

Custom Button is not being displayed

I am attempting to display a class derived from SimpleButton on a Flash screen. This button is supposed to load an external JPG file and use it as the display.
The code for the custom button is shown below:
package {
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
public class CustomState extends Sprite {
public function CustomState()
{
var loader:Loader = new Loader();
loader.load(new URLRequest("file.jpg"));
this.width = 70;
this.height = 100;
addChild(loader);
trace("State Width: "+this.width);
}
}
}
The button state code is shown below:
package {
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
public class CustomState extends Sprite {
public function CustomState()
{
var loader:Loader = new Loader();
loader.load(new URLRequest("file.jpg"));
this.width = 70;
this.height = 100;
addChild(loader);
trace("State Width: "+this.width);
}
}
}
The code used to test the button is below:
package {
import flash.display.MovieClip;
public class TestButton extends MovieClip{
public function TestButton()
{
var button:CustomButton = new CustomButton();
button.x = 10;
button.y = 30;
addChild(button);
}
}
}
For reasons that are not clear, the button does not appear at all.
Can someone tell me how to make this button appear? As can be seen from the test class, I am adding the button to its display list. I also note that despite the fact that I am setting width and height for the sprite, its width and height apparently aren't being set. Something funky is going on, but my attempts to find a piece of working code that does what I am trying to do have failed.
Someone please advise...

Problems with STAGE

I am pretty new to as3 programming. This forum helped me already a lot, but now I have a problem where I don't know how to get on. So this is my first Post on stack overflow.com.
I need StageWebView for displaying a PDF-document. After several hours, I was successful. I created the code in a new blank document and tested it step by step.
This is my code:
import flash.display.MovieClip;
import flash.media.StageWebView;
import flash.geom.Rectangle;
import flash.filesystem.File;
import flash.display.Sprite;
import flash.display.Stage;
public function StageWebViewExample(pdfdoc:String, xpos:Number, ypos:Number, breite:Number, hoehe:Number)
{
var webView:StageWebView = new StageWebView();
webView.stage = this.stage; //PROBLEM LINE
webView.viewPort = new Rectangle (xpos, ypos, breite, hoehe);
var file:String = pdfdoc;
var pdf:File = File.applicationDirectory.resolvePath(file);
webView.loadURL(pdf.nativePath);
}
StageWebViewExample("test.pdf", 200, 200, 600, 1200);
After testing, I copied the code in my existing flash-document. (The code in a several as-File and the "calling" (StageWebViewExample("....) in the existing flash-document...)
But now the code does't work anymore and there are the following Errors:
- 1119 Access of possibly undefined property stage...
- 1059 Property is read-only.
--> Both Errors referring to the same line I marked in the Code.
Has anyone an idea why it don't work?
I would really appreciate a good hint!
Answer for question is your actions ;) You moved code from the Document Class, in another one, that don't have access to the stage as instance property. I mean this.stage.
Pass Stage also to the method StageWebViewExample, function signature will look like:
public function stageWebViewExample(stage: Stage, pdfdoc:String, xpos:Number, ypos:Number, breite:Number, hoehe:Number):void {
var webView:StageWebView = new StageWebView();
webView.stage = stage;
webView.viewPort = new Rectangle (xpos, ypos, breite, hoehe);
var file:String = pdfdoc;
var pdf:File = File.applicationDirectory.resolvePath(file);
webView.loadURL(pdf.nativePath);
}
Working AIR example:
package {
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.media.StageWebView;
public class StackOverflow extends Sprite {
public function StackOverflow() {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
start();
}
private function start():void {
showWebViewAt(this.stage, "http://www.iab.net/media/file/VASTv3.0.pdf", new Rectangle(0, 0, stage.stageWidth * 0.5, stage.stageHeight));
}
private function showWebViewAt(stage:Stage, path:String, frame:Rectangle):void {
var webView:StageWebView = new StageWebView();
webView.stage = stage;
webView.viewPort = frame;
webView.loadURL(path);
}
}
}

Drawing on Live Video in Flex - Snapshot issue

I am working on a project, where I have to make an annotation video player!
Basically, I followed this tutorial[1] where the author presents how to draw(in this case just a rectangle) on the own web camera, using Flex.
Everything went great, but now I want to add a listener to get a snapshot of the image(what I draw + the web camera image) and then save it on my computer.
I have created the listener, but the problem is that this listener saves only the image from the web camera without my drawing(even though I added that draw to the camera).
private function save():void {
var bitmapData:BitmapData = new BitmapData(videoDisplay.width,videoDisplay.height);
bitmapData.draw(videoDisplay);
var ba:ByteArray = (new PNGEncoder()).encode(bitmapData);
(new FileReference()).save(ba, "doodle.png");
}
I don't have experience at all with Flex/Flash so maybe I did smth wrong.
Can you please help me?
[1]http://narinderkumar.wordpress.com/2012/02/16/drawing-on-live-video-in-flex/
this example works fine - i dont see the problem in that codesnippet of yours but maybe you are just drawing vis graphics on the video, in thatcase the graphics layer is behind the camera... hope hat helps ;)
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Camera;
import flash.media.Video;
public class cameratest extends Sprite
{
private var holder:Sprite;
private var layover:Sprite;
private var display:Sprite;
public function cameratest()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
var cam:Camera = Camera.getCamera();
var video:Video =new Video(cam.width, cam.height);
video.attachCamera(cam);
//holder hollds the video and on top the layover to draw on
holder = new Sprite();
stage.addChild(holder);
holder.addChild(video);
//layover to draw on
layover = new Sprite();
holder.addChild(layover);
//will show the snapshot
display = new Sprite();
display.x= 350;
stage.addChild(display);
//listener for onclick do snapshot
stage.addEventListener(MouseEvent.CLICK, drawAndSave);
}
private function drawAndSave(E:Event=null):void{
layover.graphics.beginFill(0xff0000,1);
layover.graphics.drawCircle(10,10,10);
var bmd:BitmapData=new BitmapData(holder.width,holder.height);
bmd.draw(holder);
var bmp:Bitmap = new Bitmap(bmd,'auto',true);
display.addChild(bmp);
}
}
}

AS3 MovieClip not playing consistently

So at the beginning when my SWF loads up it also loads up a sequence of animated clips like so:
var loader:Loader = new Loader();
loader.load(new URLRequest("clips/clip4.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, clip4Loaded);
And my clipLoaded function is:
private function clip4Loaded(e:Event):void {
clip4 = e.target.content as MovieClip;
}
The clip4 file being loaded in has a stop() at the first frame. Later in the game (clip4 is the "outro") I use:
clip4.gotoAndPlay(0);
clip4.addFrameScript(clip4.totalFrames - 1, clip4End);
However, the clip only seems to play about 25% of the time and all the other clips which I load the exact same way play fine. The only difference is that those clips get played fairly soon after they load which leads me to believe clip4 is being autoreleased at some point but I really have no clue.
strange - i've got a timeline-animated swf with stop(); in the first frame and the following code:
package {
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
/**
*
* #author www0z0k
*/
[SWF(width='400', height='300', frameRate='30')]
public class NewClass extends Sprite {
private const URL:String = 'res/1.swf';
private var spr:MovieClip;
public function NewClass():void {
var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.INIT, onloaded);
ldr.load(new URLRequest(URL));
}
private function onloaded(e:Event):void {
spr = e.target.content as MovieClip;
addChild(spr);
spr.addFrameScript(spr.totalFrames - 1, function():void { x = x == 0 ? 100 : 0; } );
spr.addEventListener(MouseEvent.CLICK, onclick);
}
private function onclick(e:MouseEvent):void {
spr.gotoAndPlay(0);
}
}
}
and it works exactly as it's written.
could you please upload your clip4.swf anywhere (or test this code with it yourself)?