Fading out volume on a movieClip - actionscript-3

I've looked around the net on this issue, and came up with the following code to fade out the volume on my movieclip:
var myTransform = new SoundTransform();
myTransform.volume = 1;
loaderClip2[indexNumber].soundTransform = myTransform;
audioTween = new TweenLite(myTransform, 2, {volume:0});
My movie clip is stored in the Array loaderClip2 at index position determined by the variable indexNumber. This code does not produce the desired fade. Can anyone see what is the problem here?

var myTransform:SoundTransform = new SoundTransform(1);
TweenLite.to(myTransform, 1, {volume:0, onUpdate:updateChannel, onUpdateParams:[indexNumber]});
function updateChannel(index:int):void {
loaderClip2[index].soundTransform = myTransform;
}

Try this code:
private function updateChannel() : void {
var st : SoundTransform = new SoundTransform(loaderClip2[indexNumber].soundTransform.volume, 0 );
loaderClip2[indexNumber].soundTransform = st;
}
TweenLite.to(loaderClip2[indexNumber], 4, { volume:.5, ease:Strong.easeInOut, onUpdate:updateChannel } );
Set your own parameters

Alright guys, after trying everything possible with tweenlite, I figured out another solution using good-old-fashioned ENTER_FRAME events. This is as straight-forward as possible, wish I had thought of it before:
so in a previous function I just do this:
myClip.addEventListener(Event.ENTER_FRAME, fadeAudio);
and then later flush out the event function (or whatever it is called):
var audioshift = 1;
function fadeAudio(e : Event) : void {
audioshift -= .05;
if (audioshift <= 0) {
audioshift = 0;
trace("fadeAudio complete");
e.target.removeEventListener(Event.ENTER_FRAME, fadeAudio);
}
var st : SoundTransform = new SoundTransform(audioshift, 0);
e.target.soundTransform = st;
}
Easy as pie.

Related

Moving object in AS3 while it is translated by Matrix3D

First, please look at my SWF: http://krakow45.pl/spec/warcaby/Warcaby3D.html
You can move pawns and it works pretty well. But the problem starts whem you translate game board (by pressing any of direction keys). After this you cant move pawns. Here is little piece of my code:
translation:
case Keyboard.LEFT:
_matrix = new Matrix3D();
_matrix.appendTranslation(0, -200, 0);
_matrix.appendRotation(_rot++, Vector3D.X_AXIS);
_matrix.appendTranslation(0, 200, _depth);
_table._board.transform.matrix3D = _matrix;
break;
moving the pawn:
private function MouseDown(event:MouseEvent):void
{
var pawn:Pawn = event.currentTarget as Pawn;
_xPos = pawn._xPos;
_yPos = pawn._yPos;
_txt.text = pawn._xPos + " - " + pawn._yPos + "\n";
pawn.startDrag();
}
Ok, I solved this by using something like this: (against startStag() )
private var _clicked:Boolean
private var _currentPawn:Pawn
private function MouseDown(event:MouseEvent):void
{
_clicked = true;
_currentPawn = event.currentTarget as Pawn;
// rest of my code
}
private function MouseMove(event:MouseEvent):void
{
if(_clicked)
{
_currentPawn.x = mouseX;
_currentPawn.y = mouseY;
}
}
private function MouseUp(event:MouseEvent):void
{
_clicked = false;
// rest of my code
}

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.

use 1 object multiple times in as3?

I'm trying to make something like bookmarks, I have 1 note on the stage and when the user clicks it, it starts to drag and the users drops it where they want. the problem is I want these notes to be dragged multiple times.. here is my code:
import flash.events.MouseEvent;
//notess is the instance name of the movie clip on the stage
notess.inputText.visible = false;
//delet is a delete button inside the movie clip,
notess.delet.visible = false;
//the class of the object i want to drag
var note:notes = new notes ;
notess.addEventListener(MouseEvent.CLICK , newNote);
function newNote(e:MouseEvent):void
{
for (var i:Number = 1; i<10; i++)
{
addChild(note);
//inpuText is a text field in notess movie clip
note.inputText.visible = false;
note.x = mouseX;
note.y = mouseY;
note.addEventListener( MouseEvent.MOUSE_DOWN , drag);
note.addEventListener( MouseEvent.MOUSE_UP , drop);
note.delet.addEventListener( MouseEvent.CLICK , delet);
}
}
function drag(e:MouseEvent):void
{
note.startDrag();
}
function drop(e:MouseEvent):void
{
e.currentTarget.stopDrag();
note.inputText.visible = true;
note.delet.visible = true;
}
function delet(e:MouseEvent):void
{
removeChild(note);
}
any help will be appreciated.
You need to create a new instance of your note class when you drop, copy the location and other variables from the note you were dragging, add your new note to the stage, and return the dragging note to its original position.
Something like:
function drop($e:MouseEvent):void
{
$e.currentTarget.stopDrag();
dropNote($e.currentTarget as Note);
}
var newNote:Note;
function dropNote($note:Note):void
{
newNote = new Note();
// Copy vars:
newNote.x = $note.x;
newNote.y = $note.y;
// etc.
// restore original note.
// You will need to store its original position before you begin dragging:
$note.x = $note.originalX;
$note.y = $note.orgiinalY;
// etc.
// Finally, add your new note to the stage:
addChild(newNote);
}
... this is pseudo-code really, since I don't know if you need to add the new note to a list, or link it to its original note. If you Google ActionScript Drag Drop Duplicate, you will find quite a few more examples.
I think you are not target the drag object in drag function and problem in object instantiation
for (var i:Number = 1; i<numberOfNodes; i++) {
note = new note();
addChild(note);
...
....
}
function drag(e:MouseEvent):void{
(e.target).startDrag();
}
If you are dragging around multiple types of objects (eg. Notes and Images), you could do something like this, rather than hard coding the type of object to be instantiated.
function drop(e:MouseEvent):void{
// Get a reference to the class of the dragged object
var className:String = flash.utils.getQualifiedClassName(e.currentTarget);
var TheClass:Class = flash.utils.getDefinitionByName(className) as Class;
var scope:DisplayObjectContainer = this; // The Drop Target
// Convert the position of the dragged clip to local coordinates
var position:Point = scope.globalToLocal( DisplayObject(e.currentTarget).localToGlobal() );
// Create a new instance of the dragged object
var instance:DisplayObject = new TheClass();
instance.x = position.x;
instance.y = position.y;
scope.addChild(instance);
}

AS3 - Apply BitmapData addChild to multiple MovieClips

I need to add "_myThumb" to 4 container MovieClips. The problem is that it's only working for 1 MovieClip. What do I need to change?
var _myThumb:Bitmap;
var _myThumbData:BitmapData;
function createThumbs()
{
_myThumbData = new BitmapData(photodefault.width,photodefault.height,false,0xffffff);
_myThumb = new Bitmap(_myThumbData);
_myThumb.smoothing = true;
_myThumb.scaleX = _myThumb.scaleY = 0.2;
// Add to t1-t4 container
photothumbs.t1.addChild(_myThumb);
photothumbs.t2.addChild(_myThumb);
photothumbs.t3.addChild(_myThumb);
photothumbs.t4.addChild(_myThumb);
}
createThumbs();
function createThumbnail()
{
_myThumbData.draw(photodefault);
}
Thanks.
Uli
You need to create separate Bitmap objects for each thumb, but you can use the same source Bitmapdata for that. This is an example using a utility function to create the bitmap object:
function createThumbs()
{
_myThumbData = new BitmapData(photodefault.width,photodefault.height,false,0xffffff);
// Add to t1-t4 container
photothumbs.t1.addChild(createBitmap(_myThumbData));
photothumbs.t2.addChild(createBitmap(_myThumbData));
photothumbs.t3.addChild(createBitmap(_myThumbData));
photothumbs.t4.addChild(createBitmap(_myThumbData));
}
function createBitmap(bmd:BitmapData):Bitmap
{
var bitmap:Bitmap = new Bitmap(bmd);
bitmap.smoothing = true;
bitmap.scaleX = bitmap.scaleY = 0.2;
return bitmap;
}

AS3 child issue with if statements

Alright, can someone explain what is wrong with my code below, there is no errors, but it's not doing what I want it to do. I need it to display a movieclip on the screen when a variable called "randint", which is generated by random, is greater than or equal to 0.5. If it's not then it doesn't get displayed. Code:
addEventListener(Event.ENTER_FRAME, char_coll);
function char_coll(ev : Event) : void
{
if(currentFrame==2)
{
if (randint >= 0.5){
var w1:woman1 = new woman1();
randint = Math.random();
if(w1.hitTestObject(stand)){
w1.gotoAndPlay(1);
cash1 = cash1 + 1;
}
}
}
};
randint is set inside the if statement. This means that randint always is undefiend, because it has to be >= 0.5 to be set to any value (kind of a catch 22).
This code should work:
addEventListener(Event.ENTER_FRAME, char_coll);
function char_coll(ev : Event) : void
{
if(currentFrame==2)
{
var randint:Number = Math.random();
if (randint >= 0.5){
var w1:woman1 = new woman1();
stage.addChild(w1);
if(w1.hitTestObject(stand)){
w1.gotoAndPlay(1);
cash1 = cash1 + 1;
}
}
}
};
Then of course you have to add w1 to the stage using addChild() as you can se below var w1:woman1 = new woman1();
hope it helps!