Start Tween when other Tween reaches certain position AS3 - actionscript-3

I'm trying to create a simple slideshow of 3 images going from left to right.
I want to check if the last image is on stage ( so the image3.x = 0 ) in order to start the new Tween.
I thought it would be something a simple as the following code, but that doesn't seem to work.
function Start() {
if(image3.x == 0){
var myTween:Tween = new Tween(image, "x", None.easeNone, -140, 640, 10, true);
}
}
stage.addEventListener(Event.ENTER_FRAME, Start);

Split the original Tween in two parts, from -140 to 0 and from 0 to 640.
var tween:Tween = new Tween(image, "x", None.easeNone, -140, 0, 10, true);
Wait for it to finish:
tween.addEventListener(TweenEvent.MOTION_FINISH, onZeroReached);
Then do the second part plus whatever else you want to do:
function onZeroReached(te:TweenEvent):void
{
tween = new Tween(image, "x", None.easeNone, 0, 640, 10, true);
// do additional stuff here
}
code untested

Related

Tween functions for series of menus wont work after the 1st

I am having a problem with a series of drop down menus i'm trying to make. I have 3 buttons, each of which when hovered over, drops down a sub menu with options. The problem I'm running into is that the first menu works, it drops down as it should. The second and third menus however do not work, and I don't understand why. I'm not getting an error code, and some googling directed me to people who were having similar problems suggesting flash garbage collection might be what is causing my issue. i'm not sure how to structure it so that this wont happen. I should also add that this is my first time posting so I apologize for any format issues in the post, and I'm very new to coding so any help will be much appreciated, I don't really know the questions to ask to get me going in the right direction.
Thankyou for your time
The code I have so far is as follows
import fl.transitions.Tween;
import fl.transitions.easing.Regular;
stop();
menu_cont.paint_rect.alpha = 0;
menu_cont.trim_rect.alpha = 0;
menu_cont.wheels_rect.alpha = 0;
menu_cont.paint_btn.addEventListener(MouseEvent.ROLL_OVER, paintRollOver);
menu_cont.paint_rect.addEventListener(MouseEvent.MOUSE_OUT, paintMouseOut);
menu_cont.trim_btn.addEventListener(MouseEvent.ROLL_OVER, trimRollOver);
menu_cont.trim_rect.addEventListener(MouseEvent.MOUSE_OUT, trimMouseOut);
menu_cont.wheels_btn.addEventListener(MouseEvent.ROLL_OVER, wheelsRollOver);
menu_cont.wheels_rect.addEventListener(MouseEvent.MOUSE_OUT, wheelsMouseOut);
var paintMenuTween:Tween = new Tween (menu_cont.paint_menu, "y",
Regular.easeOut,menu_cont.paint_menu.y, 50, .5, true);
var paintMenuTween2:Tween = new Tween (menu_cont.paint_menu, "y",
Regular.easeOut,menu_cont.paint_menu.y, -15, .5, true);
var trimMenuTween:Tween = new Tween (menu_cont.trim_menu, "y",
Regular.easeOut,menu_cont.trim_menu.y, 50, .5, true);
var trimMenuTween2:Tween = new Tween (menu_cont.trim_menu, "y",
Regular.easeOut,menu_cont.trim_menu.y, -15, .5, true);
var wheelsMenuTween:Tween = new Tween (menu_cont.wheels_menu, "y",
Regular.easeOut,menu_cont.wheels_menu.y, 50, .5, true);
var wheelsMenuTween2:Tween = new Tween (menu_cont.wheels_menu, "y",
Regular.easeOut,menu_cont.wheels_menu.y, -15, .5, true);
function paintRollOver (event:MouseEvent):void {
paintMenuTween = new Tween (menu_cont.paint_menu, "y",
Regular.easeOut,menu_cont.paint_menu.y, 50, .5, true);
trace ("paint over")
}
function paintMouseOut (event:MouseEvent):void {
paintMenuTween2 = new Tween (menu_cont.paint_menu, "y",
Regular.easeOut,menu_cont.paint_menu.y, -15, .5, true);
trace ("paint out")
}
function trimRollOver (event:MouseEvent):void {
trimMenuTween = new Tween (menu_cont.trim_menu, "y", Regular.easeOut,menu_cont.trim_menu.y, 50, .5, true);
trace("trim over")
}
function trimMouseOut (event:MouseEvent):void {
trimMenuTween2 = new Tween (menu_cont.trim_menu, "y", Regular.easeOut,menu_cont.trim_menu.y, -15, .5, true);
trace("trim out")
}
function wheelsRollOver (event:MouseEvent):void {
wheelsMenuTween = new Tween (menu_cont.wheels_menu, "y", Regular.easeOut,menu_cont.wheels_menu.y, 50, .5, true);
trace ("wheels over")
}
function wheelsMouseOut (event:MouseEvent):void {
wheelsMenuTween2 = new Tween (menu_cont.wheels_menu, "y", Regular.easeOut,menu_cont.wheels_menu.y, -15, .5, true);
trace ("wheels out")
}

Render to texture not functioning

I'm trying to test Stage3D's setRenderToTexture function, but my test code is not providing any output. I was hoping someone could tell me why - I've tried practically everything to get this working. The code I've created is below:
import flash.events.Event;
import flash.display.*;
import flash.display3D.*;
import flash.display3D.textures.Texture;
import AGALMiniAssembler;
//using stage size in several locations for simplicity - needs to be square, 2^n
[SWF(width='512', height='512', backgroundColor='0x000000', frameRate='60')]
//vars
var context0:Context3D;
var vBuff:VertexBuffer3D;
var iBuff:IndexBuffer3D;
var tex0:Texture;
var tex1:Texture;
var vShader:AGALMiniAssembler = new AGALMiniAssembler();
var fShader:AGALMiniAssembler = new AGALMiniAssembler();
var program:Program3D;
//create a square
var square:Shape = new Shape();
square.graphics.beginFill(0xaa00ff,1);
square.graphics.drawRect(10, 10, stage.stageWidth - 20, stage.stageHeight - 20);
//draw square to bitmapdata and create one blank bitmapdata
var tex0data:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false,0);
tex0data.draw(square);
var tex1data:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false,0);
//initialize stage3d
stage.stage3Ds[0].addEventListener(Event.CONTEXT3D_CREATE, initStage3D);
stage.stage3Ds[0].requestContext3D();
function initStage3D(e:Event):void {
context0 = stage.stage3Ds[0].context3D;
context0.configureBackBuffer(stage.stageWidth, stage.stageHeight, 0);
//create buffers - simple quad
var vBuff_vec:Vector.<Number> = new <Number>[-1, -1, 0, 0, 1,
-1, 1, 0, 0, 0,
1, 1, 0, 1, 0,
1, -1, 0, 1, 1];
var iBuff_vec:Vector.<uint> = new <uint>[0, 1, 2, 0, 2, 3]
vBuff = context0.createVertexBuffer(4, 5);
vBuff.uploadFromVector(vBuff_vec, 0, 4);
iBuff = context0.createIndexBuffer(6);
iBuff.uploadFromVector(iBuff_vec, 0, 6);
//load bitmapdata into textures - square to tex0 and blank to tex1
tex0 = context0.createTexture(tex0data.width,tex0data.height,'bgra',false);
tex0.uploadFromBitmapData(tex0data);
tex1 = context0.createTexture(tex1data.width, tex1data.height,'bgra',true);
tex1.uploadFromBitmapData(tex1data);
//create and upload simple shader program
vShader.assemble('vertex', 'mov v0, va1 \n'+ //uv coords to v0
'mov op, va0'); //output xyz coords
fShader.assemble('fragment', 'tex oc, v0, fs0 <2d, linear, nomip>'); //output texture at fs0
program = context0.createProgram();
program.upload(vShader.agalcode, fShader.agalcode);
context0.setProgram(program);
//set buffers
context0.setVertexBufferAt(0, vBuff, 0, 'float3');
context0.setVertexBufferAt(1, vBuff, 3, 'float2');
//prep rendering
addEventListener(Event.ENTER_FRAME, render);
}
function render(e:Event):void {
context0.clear();
//render tex0 onto tex1
context0.setRenderToTexture(tex1);
context0.setTextureAt(0, tex0);
context0.drawTriangles(iBuff);
//render tex1 to back buffer and present
context0.setTextureAt(0, tex1);
context0.setRenderToBackBuffer();
context0.drawTriangles(iBuff);
context0.present();
}
EDIT: I have noticed that changing the color of the blank bitmap does change the color of the final output - it's being displayed, but the square isn't being rendered to it.
FINALLY figured this out! There is a subtle, but important, step that must be taken before a texture is viable as a render target - it must be cleared after the render target is switched. Revising the render function as follows causes the program to function as expected:
function render(e:Event):void {
//render tex0 onto tex1
context0.setRenderToTexture(tex1);
context0.clear();
context0.setTextureAt(0, tex0);
context0.drawTriangles(iBuff);
//render tex1 to back buffer and present
context0.setTextureAt(0, tex1);
context0.setRenderToBackBuffer();
context0.clear();
context0.drawTriangles(iBuff);
context0.present();
}

As3 draw bitmap + copyPixel around mouseX and mouseY

I am doing a magnifying glass effect?
I have a working version. However the performance is not good enough on the tablet.
What i have done so far:
I had a ENTERFRAME event on a mouseDown
So it start capture the screen when the mouse click down, and follow the mouseX and mouseY
It works, but the only problem it keep draw the whole stage rather than maybe (300px * 300px) around the mouseX and mosueY. is there a way i can make the draw area according to your mouseX and mouseY. I guess that would help the performance as well. :)
e.target.removeEventListener(Event.ENTER_FRAME, startCapture);
function startCapture(e:Event):void{
var glassWidth:uint=80;
var glassHeight:int=80;
var curBd:BitmapData;
var curBmp:Bitmap;
var posX:int = _parentStage.mouseX - 40;
var posY:int = _parentStage.mouseY - 40;
//-------------------------------------------------------------
//var subArea:Rectangle = new Rectangle(0,0,500,500);
//var newBmp:Bitmap = new BitmapData(500,500);
//var cutoutBmp:Bitmap = new Bitmap( newBmp, PixelSnapping.ALWAYS, true );
//cutoutBmp.bitmapData.draw( jpgSource, new Matrix(1, 0, 0, 1, -357, -341) , null, null, subArea, true );
//-------------------------------------------------------------
bd = new BitmapData(1024, 768, true, 0);
var subArea:Rectangle = new Rectangle(_parentStage.mouseX, _parentStage.mouseY, 500, 500);
// bd = new BitmapData(500, 500);
bd.draw(_parentStage.mc_mainContainer);
// bd.draw(_parentStage.mc_mainContainer);
curBmp=new Bitmap(new BitmapData(glassWidth,glassHeight), PixelSnapping.ALWAYS);
curBmp.bitmapData.copyPixels(bd,new Rectangle(posX,posY,_parentStage.mouseX,_parentStage.mouseY),new Point(0,0));
curBd=curBmp.bitmapData;
var ma:Matrix = new Matrix(1, 0, 0, 1, -40, -40);
glass.name = 'glass';
glass.alpha = 1;
glass.graphics.clear();
glass.graphics.beginBitmapFill(curBd, ma);
glass.graphics.drawCircle(0, 0, 35);
//glass.graphics.drawCircle(0, 0, 35);
glass.graphics.endFill();
//var imageCircle:Bitmap = new _magGlass();
//trace(_magGlass);
//glass.addChild(_magGlass);
if(!_parentStage.contains(glass))_parentStage.addChildAt(glass, _parentStage.numChildren - 2);
glass.x = _parentStage.mouseX;
glass.y = _parentStage.mouseY - 75;
}
Where your performance problem is coming from is where you're creating new bitmaps and bitmapdatas and rectangles on every frame. Instead, just create one or two in the constructor or on click and then reuse them. Since you already have a BitmapData, you can probably just put that on the stage and mask it, rather than using the graphics object in glass. At least pick one approach or the other. Doing both is killing you, performance wise.

Flash Action Script 3 rotate button

Could anyone tell me whats wrong with this code? I'm attempting to rotate a button in action script 3 and i keep getting the error:
ArgumentError: Error #2025: The supplied DisplayObject must be a child
of the caller. at flash.display::DisplayObjectContainer/removeChild()
at
distributor_app_fla::MainTimeline/NewChartOptionsReturn()[distributor_app_fla.MainTimeline::frame1:218]
at
distributor_app_fla::MainTimeline/ClickNewChartOptions()[distributor_app_fla.MainTimeline::frame1:101]
I've already Googled the error and everything i read told me to remove the child then re-add it to the frame but it continues to break at the same spot.
code:
//defined
var btnNewChartOptions:NewChartOptions = new NewChartOptions();
btnNewChartOptions.y = 279;
btnNewChartOptions.x = 439;
//created
function NewChartDown():String
{
btnNewChartOptions.addEventListener(MouseEvent.CLICK, ClickNewChartOptions);
btnNewChartOptions.alpha = 0;
addChild(btnNewChartOptions);
var NewChartOptionsTween:Tween = new Tween(btnNewChartOptions, "alpha", Strong.easeOut, 0, 1, 1, true);
return "NewChartSelected";
}
//actual code on button
function NewChartOptionsDown():String
{
rightGrayOut.alpha = 0;
addChild(rightGrayOut);
var grayOutTween:Tween = new Tween(rightGrayOut, "alpha", Strong.easeOut, 0, 1, 1, true);
var rotateTween:Tween = new Tween(btnNewChartOptions, "rotation", Strong.easeOut, 0, 180, 1, true);
return "NewChartOptions";
}
any help is appreciated!
There will be no need to remove and re-add the object because it is either present and available to the method or not, meaning if it were available to the method for removal then it would also be available for addressing and manipulation. This sounds a whole lot like a scope/visibility issue.
This code is on frame 1 on the timeline, which means that it should be able to address anything that's directly on the stage or assigned to a variable that is in the same scope at the time the function is run. (In the _root scope if this was AS2, like the methods on frame 1)
Is btnNewChartOptions inside another DisplayObject, like a sprite you use to hold all your buttons or something, as opposed to sitting directly on the stage? Maybe you can describe the object heirarchy you are trying to address as well as how the button gets attached to the stage (e.g. instantiated and attached at runtime or placed on the stage in a keyframe).
Can you provide the error that was being thrown before the add and remove fix was attempted?
If looks like to me the object has not been added yet to the display list.
// replace the following lines
removeChild(btnNewChartOptions);
addChild(btnNewChartOptions);
// with
if( !this.contains( btnNewChartOptions ) ){
return "";
}
[EDIT]
Your code.
//var created
var btnNewChartOptions:NewChartOptions = new NewChartOptions();
btnNewChartOptions.y = 279;
btnNewChartOptions.x = 439;
//button code clicked that creates button
function NewChartDown():String {
btnNewChartOptions.addEventListener(MouseEvent.CLICK, ClickNewChartOptions);
btnNewChartOptions.alpha = 0;
addChild(btnNewChartOptions);
var NewChartOptionsTween:Tween = new Tween(btnNewChartOptions, "alpha", Strong.easeOut, 0, 1, 1, true);
return "NewChartSelected";
}
//function for button
function NewChartOptionsDown():String {
rightGrayOut.alpha = 0;
addChild(rightGrayOut);
var grayOutTween:Tween = new Tween(rightGrayOut, "alpha", Strong.easeOut, 0, 1, 1, true);
var rotateTween:Tween = new Tween(btnNewChartOptions, "rotation", Strong.easeOut, 0, 180, 1, true);
return "NewChartOptions";
}
In your code you provided you are only doing addChild(btnNewChartOptions); in the NewChartDown function.
So that tells me that NewChartOptionsDown can not be called until NewChartDown has been called first. Because the btnNewChartOptions has never been added.
What you can do is move addChild(btnNewChartOptions); outside of the function.
//var created
var btnNewChartOptions:NewChartOptions = new NewChartOptions();
btnNewChartOptions.y = 279;
btnNewChartOptions.x = 439;
addChild(btnNewChartOptions);

as3 tween position relative to current position

I have a tween set on a timer:
var manTimer:Timer = new Timer(1000,14);
manTimer.addEventListener(TimerEvent.TIMER, moveMan);
function moveMan(e:TimerEvent):void {
var manX:Tween = new Tween(man, "x", Regular.easeOut, 0, -40, 1, true);
}
I just need to make the tween's position relative to it's current position, as opposed to starting at the stage's 0 position then moving the the stage's -40 position. It needs to start at its current position the move -40 from that position.
Thanks,
Wade
var manX:Tween = new Tween(man, "x", Regular.easeOut, man.x, man.x - 40, 1, true);