Draw Rentangle inside a MovieClip ActionScript 3 - actionscript-3

I have a map consisting of MovieClip cities inside it and I have a click function in top layer of map MovieClip. I try to do that if I click a city, a rectangle will be drawn. Here is code:
function rpress(a)
{
trace( "trying" );
var b:MovieClip = new MovieClip();
b.graphics.beginFill(0xFF0000);
b.graphics.drawRect(0,0,100,80);
b.graphics.endFill();
b.x = 150;
b.y = 150;
addChild( b );
trace("done")
}
Trace commands are executed but no rectangle is drawn. I tried MovieClip( root ).addChild..., stage.addChild..., MovieClip( parent ).addChild... and others...
Do you have any idea? Thank you!
FULL CODE:
Double Click map MovieClip->84 layers welcome us->Chose the layer named "Action Layer" ACTIONS-FRAME:
function rbtxt(a)
{
var _loc2 = a;
var _loc3 = this;
balon._visible = true;
arbtxt = ilad.split(",");
balon.txt.text = arbtxt[_loc2];
_loc3["x" + _loc2].play();
balon._x = _loc3["x" + _loc2]._x;
balon._y = _loc3["x" + _loc2]._y - _loc3["x" + _loc2]._height / 2 + 5;
}
//End of the function
function rbalon(a)
{
balon._visible = false;
this["x" + a].gotoAndStop(1);
}
//End of the function
function rpress(a)
{
trace( "trying" );
var b:MovieClip = new MovieClip();
b.graphics.beginFill(0xFF0000);
b.graphics.drawRect(0,0,100,80);
b.graphics.endFill();
b.x = 150;
b.y = 150;
addChild(b );
trace("done")
}
ilad = "CITY NAMES....."
ilurl = "CITY URLS....."

You are not showing enough code, so we can only guess. By looking at the info you supplied you could try:
this["x" + a].addChild( b );
or
balon.addChild( b );
But this is only guessing...

Hi try to add the movie clip to the stage and then execute the drawing camamnd like this
var b:MovieClip = new MovieClip();
addChild(b );
b.graphics.beginFill(0xFF0000);
b.graphics.drawRect(0,0,100,80);
b.graphics.endFill();
b.x = 150;
b.y = 150;

Sprite has access to graphics, try and only use Movieclip for complex objects such as SWFs loaded in and Flash library assets with timeline based animation and variables etc
function drawRect()
{
trace( "trying" );
//var b:MovieClip = new MovieClip();
var b:Sprite = new Sprite();
b.graphics.beginFill(0xFF0000);
b.graphics.drawRect(0,0,100,80);
b.graphics.endFill();
b.x = 150;
b.y = 150;
addChild( b );
trace("done")
}

Related

as3 totalFrames return 1

I am trying to load one animation.
I want to know the end of the animation and avoid it in a loop.
var myLoader:Loader = new Loader( );
var myLoaderReq:URLRequest = new URLRequest("anim.swf");
myLoader.load(myLoaderReq);
addEventListener(Event.ENTER_FRAME , enterFrameListener );
addChild(myLoader);
myLoader.x = 10;
myLoader.y = 20;
function enterFrameListener(event:Event):void
{
var mc:MovieClip = event.currentTarget as MovieClip;
trace(mc.currentFrame);
if( mc.currentFrame == mc.totalFrames )
{
mc.removeEventListener(Event.ENTER_FRAME , enterFrameListener );
trace("end of movie reached");
}
}
for test i uses swf downloaded from http://www.leconcombre.com/movies/movies1us.html
any ideas how to do that?
thnx
The currentTarget you are using is refer to
addEventListener(Event.ENTER_FRAME , enterFrameListener );
Which should be your MainTimeline, if you add one more frame then should be return 2 of totalFrames.
As BadFeelingAboutThis saids, just replace:
var mc:MovieClip = event.currentTarget as MovieClip;
by
var mc:MovieClip = myLoader.content as MovieClip;
You also have to confirm you are attempting to manipulate the MainTimeline of anim.swf.

The movieclip contains various examples of names

How do I loaded MovieClips in the position of the mouse with a different name on each click? or I have to make several different MovieClips to laod?
please help me :)
Is this what you're trying to do?
var counter:int = 0;
stage.addEventListener(MouseEvent.CLICK, click);
function click(event:MouseEvent):void
{
var mc:MovieClip = new MovieClip(); // Or new YourExportedLibraryClip();
mc.name = 'mc_' + (counter++);
mc.x = stage.mouseX;
mc.y = stage.mouseY;
stage.addChild(mc);
}

Remove sprite from stage in as3

I have this code for a message to appear on stage when player finishes drag and drop. I would like this sprite to be removed when a button is clicked for the next frame. Can someone help me with the code?
stage.addEventListener(Event.ENTER_FRAME, EntFrame);
function EntFrame (e:Event):void
{
if (CntP1+CntP2+CntP3+CntP4+CntP5+CntP6+CntP7+CntP8 == 40)
{
var w:int = 400, h:int = 200;
var win:Sprite = new Sprite();
win.name = "Mywin";
addChild(win);
// draw rounded rect with subtle vertical linear gradient fill and blue stroke
win.graphics.lineStyle(4,0x0077ff);
var mat:Matrix = new Matrix();
mat.createGradientBox(w, h, 90 * (Math.PI / 180));
win.graphics.beginGradientFill(GradientType.LINEAR,[0xffffff,0xeeeeee],[1.00,1.00],[0,255],mat);
win.graphics.drawRoundRect(0,0,w,h,15,15);
// show center "YOU WIN!" text
var tf:TextField = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
tf.antiAliasType = AntiAliasType.ADVANCED;
tf.defaultTextFormat = new TextFormat("Arial, Verdana",36,0x454545,true);
tf.text = "Κέρδισες!";
tf.selectable = false;
win.addChild(tf);
tf.x = w/2 - tf.width/2;
tf.y = h/2 - tf.height/2;
// add a drop shadow
var dropShadow:DropShadowFilter = new DropShadowFilter(3,45,0,.35,8,8,1,3);
win.filters = [dropShadow];
// center the graphic
win.x = stage.stageWidth/2 - win.width/2;
win.y = stage.stageHeight/2 - win.height/2;
}
}
Your code isn't written well and needs rewriting to ensure reuse or scalability of your project, but here's a quick way out.
make a holder Sprite, something like
var messageHolder:Sprite = new Sprite();
addChild(messageHolder);
add all the messages to that holder in any fashion you like. When you need to erase the contents of that holder, call following method:
function clearHolderContents(holder:DisplayObjectContainer):void
{
if (holder.numChildren < 1)
return; // no need to continue this method if the target is empty
for (var i:int = holder.numChildren - 1; i >= 0; i--)
removeChild(holder.getChildAt(i));
}
This method can clear contents of any DisplayObjectContainer => use it for your messageHolder:
clearHolderContents(messageHolder);
Hope that helps!

Game conversion from AS2 to AS3

I have an example of very simple shooter. But it on AS2. Here the source:
speed = 4;
depth = 0;
nose = 50;
_root.onMouseMove = function() {
updateAfterEvent();
xdiff = _root._xmouse-spaceShip._x;
ydiff = _root._ymouse-spaceShip._y;
angle = Math.atan2(ydiff, xdiff);
angle = angle*180/Math.PI;
spaceShip._rotation = angle;
};
_root.onMouseDown = function() {
angle = spaceShip._rotation;
angle = angle*Math.PI/180;
++depth;
name = "projectile"+depth;
_root.attachMovie("projectile", name, depth);
//projectile - it is bullet
_root[name]._x = spaceShip._x+nose*Math.cos(angle);
_root[name]._y = spaceShip._y+nose*Math.sin(angle);
_root[name].xmov = speed*Math.cos(angle);
_root[name].ymov = speed*Math.sin(angle);
_root[name].onEnterFrame = function() {
this._x += this.xmov;
this._y += this.ymov;
};
};
I want to do the same, but in as3.
I tried to convert. Here is what I have: PS - I'm newbie, please, don't get angry of the code below :)
var nose=55;
var angle;
var acc=1;
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE, cursor);
function cursor(e:MouseEvent):void {
cross.x=mouseX;
cross.y=mouseY;
}
stage.addEventListener(Event.ENTER_FRAME, rotation2);
function rotation2(event:Event):void {
var xdiff=mouseX-spaceShip.x;
var ydiff=mouseY-spaceShip.y;
angle=Math.atan2(ydiff,xdiff);
angle=angle*180/Math.PI;
spaceShip.rotation=angle;
}
stage.addEventListener(MouseEvent.CLICK, shoot);
function shoot(event:MouseEvent):void {
angle = spaceShip.rotation;
angle = angle*Math.PI/180;
bullet.x=spaceShip.x+nose*Math.cos(angle);
bullet.y=spaceShip.y+nose*Math.sin(angle);
var xmov=acc*Math.cos(angle);
var ymov=acc*Math.sin(angle);
stage.addEventListener(Event.ENTER_FRAME, action);
function action(event:Event):void {
bullet.x+=xmov;
bullet.y+=xmov;
}
}
bullet appears, but only once, and did not move on the right path.
how to do that would be a lot of bullets, as in the example above?
attachMovie() is not the same as addChild().
MovieClip.attachMovie() creates a new symbol and add it to the MovieClip
DisplayObjectContainer.addChild() adds the specified DisplayObject to the container
Instead of calling (in AS2):
_root.attachMovie("projectile", name, depth);
You should use something like this (in AS3):
var proj:DisplayObject = new projectile();
proj.name = "projectile" + depth;
stage.addChild(proj);
Please note that there is no depth in AS3.
You could trick this by using addChildAt().
Thanks for the fast replying. I solve the problem this way:
I added this:
var bullet1:Bullet = new Bullet ();
addChild (bullet1);
And changed all "bullet" below on "bullet1".
Program is working now correctly.

"Error#2025 The supplied DisplayObject must be a child of the caller." upon trying to removeChild();

I'm creating a simple iPhone game (but the class I'm in is requiring me to use Flash...), and I'm having a major issue as I'm trying to remove objects from the game.
The game is somewhat of a dynamic time-wasting bubblewrap game where there are infinite "refreshes" of the stage. Once the user pops all the bubbles, more randomly generate. (When the array is empty, call the setup() again.)
For now, I have one of each five different colored bubble graphics pulled from the library by code and placed on the stage randomly. However, I don't like the overlap, so I've given each bubble a small square hitspace so when they're placed randomly and overlap significantly they pop themselves. For some reason when I'm trying to remove the bubbles it gives me
Error#2025 The supplied DisplayObject must be a child of the caller.
I've tried adding stage to addChild and removeChild, but I get the same error.
import flash.events.*;
import flash.display.*;
var bubbles:Array = new Array();
var b:BlueBubble = new BlueBubble();
var b2:GreenBubble = new GreenBubble();
var b3:PinkBubble = new PinkBubble();
var b4:PurpleBubble = new PurpleBubble();
var b5:YellowBubble = new YellowBubble();
// values to be tweaked later
var bNum:uint = 1;
var maxSize:uint = 100;
var minSize:uint = 1;
var range:uint = maxSize - minSize;
var tooBig:uint = 100;
function init():void {
setup();
stage.addEventListener(Event.ENTER_FRAME, onEveryFrame);
}
function setup():void {
for (var i:uint=0; i<bNum; i++) {
// blue
b.width = Math.ceil(Math.random() * range) + minSize;
b.height = b.width;
b.x = Math.random()*(stage.stageWidth - b.width);
b.y = Math.random()*(stage.stageHeight - b.height);
bubbles.push(b);
stage.addChild(b);
// green
b2.width = Math.ceil(Math.random() * range) + minSize;
b2.height = b2.width;
b2.x = Math.random()*(stage.stageWidth - b2.width);
b2.y = Math.random()*(stage.stageHeight - b2.height);
bubbles.push(b2);
stage.addChild(b2);
// pink
b3.width = Math.ceil(Math.random() * range) + minSize;
b3.height = b3.width;
b3.x = Math.random()*(stage.stageWidth - b3.width);
b3.y = Math.random()*(stage.stageHeight - b3.height);
bubbles.push(b3);
stage.addChild(b3);
// purple
b4.width = Math.ceil(Math.random() * range) + minSize;
b4.height = b4.width;
b4.x = Math.random()*(stage.stageWidth - b4.width);
b4.y = Math.random()*(stage.stageHeight - b4.height);
bubbles.push(b4);
stage.addChild(b4);
// yellow
b5.width = Math.ceil(Math.random() * range) + minSize;
b5.height = b5.width;
b5.x = Math.random()*(stage.stageWidth - b5.width);
b5.y = Math.random()*(stage.stageHeight - b5.height);
bubbles.push(b5);
stage.addChild(b5);
//add event listeners for bubbles later
}
}
function onEveryFrame(e:Event) {
for (var i:uint=0; i<bubbles.length; i++) {
bubbles[i].width++;
bubbles[i].height++;
if (bubbles[i].height >= tooBig && bubbles[i].width >= tooBig) {
bubbles[i].height = tooBig;
bubbles[i].width = tooBig;
}
// Blue hit testing
if (b2.hitGreen.hitTestObject(b.hitBlue)) {
removeChild(b);
}
if (b3.hitPink.hitTestObject(b.hitBlue)) {
removeChild(b);
}
if (b4.hitPurple.hitTestObject(b.hitBlue)) {
removeChild(b);
}
if (b5.hitYellow.hitTestObject(b.hitBlue)) {
removeChild(b);
}
// Other hit testing...
}
}
init();
You are adding b, b2, b3, etc to the stage, but removing them from whatever object is executing this code. Try changing
stage.addChild(b);
to
addChild(b);
Additionally, it is a good idea to check that an object is a child before removing it. Try changing
if (...)
removeChild(b);
to
if (contains(b) && ...)
removeChild(b);
The best way to solve this is:
mc.parent.removeChild(mc);
Basically, the removeChild function needs to be called from whatever display object that your soon to be removed object is in.