ActionScript Bitmap Filter Tweening - actionscript-3

i can't seem to tween any bitmap filters. here's my code:
var dropShadow:DropShadowFilter = new DropShadowFilter();
mySprite.filters = [dropShadow];
var dropShadowTween:Tween = new Tween(dropShadow, "distance", Regular.easeOut, 4.0, 20, 2, true);
what is my mistake? i've also tried the following but it doesn't work:
var dropShadowTween:Tween = new Tween(mySprite.filters[0], "distance", Regular.easeOut, 4.0, 20, 2, true);

the main problem with the tweening of filters is that you have to reassign them before they change, just changing the value of the dropshadow wont make a difference until you call mySprite.filters = new Array(dropshadow) again. Just incase it becomes needed further down the line!
i would personally use TweenLite by Greensock with its FilterPlugins, works a treat!

Related

How to attach a moiveClip into as3isolib grid?

I am a beginner with using AS3 and as3isolib. I am trying to add some MC from the fla library into an isoGrid object. I tried to use addChild() method of the IsoGrid class but it gave me an error saying: 1067: Implicit coercion of a value of type [MovieClip Name] to an unrelated type as3isolib.data:INode. Which I think wants me to use the node class.
Any idea how to do such a thing?
Thank you in advance.
as3isolib use it's own display list like rendering tree, add all nodes in this tree must implements as3isolib.data:INode. There are two possibility to add flash native display objects to the IsoScene:
Take a look on this small tutorial:
//create IsoView, IsoScene and IsoGrid - default from as3isolib
var view:IsoView = new IsoView();
view.setSize(stage.stageWidth, stage.stageHeight);
addChild(view);
var scene:IsoScene = new IsoScene();
view.addScene(scene);
var grid:IsoGrid = new IsoGrid({cellSize:32});
grid.setGridSize(800, 600);
grid.stroke = new Stroke(0, 0x576F33);
grid.render();
//create iso box, just for demo
var obj:IsoBox = new IsoBox();
obj.setSize(32, 32, 64);
obj.moveTo(5*32, 5*32, 1);
//first possiblity to add flash.display.Shape to the iso scene - using IsoSprite.sprites
var isoSprite:IsoSprite = new IsoSprite();
isoSprite.moveTo(5*32, 7*32, 1);
var shape1:Shape = new Shape();
shape1.graphics.beginFill(0xFF0000, 1);
shape1.graphics.drawRect(0, 0, 32, 32);
isoSprite.sprites = [shape1];
//second possiblity to add flash.display.Shape to the iso scene - using IsoDisplayObject.container
var isoObj:IsoDisplayObject = new IsoDisplayObject();
isoObj.moveTo(7*32, 7*32, 1);
var shape2:Shape = new Shape();
shape2.graphics.beginFill(0x0000FF, 1);
shape2.graphics.drawRect(0, 0, 32, 32);
isoObj.container.addChild(shape2);
//add all objects to the scene and render all
scene.addChild(grid);
scene.addChild(obj);
scene.addChild(isoSprite);
scene.addChild(isoObj);
scene.render();

Drawing a bitmap based on a transformed movieclip

I'm drawing bitmaps of movieclips which I then feed into my hittest function to test for collisions. However, I'm not quite sure how i would add to the code below to take into account and draw bitmaps for movieclips which have been scaled and/or rotated. The below code obviously only works for non-transformed movieclips. I've included in comments code which i've already tried but to no success.
When adding the drawn bitmap to the stage, no matter whether the movieclip in question is transformed or not, the drawn bitmap is "cut off" and incorrectly drawn - it appears to only draw a section of it. However, this does not particularly affect the collision testing for the non-transformed movieclips, but has an adverse effect on transformed movieclips.
All of the movieclips I want to be drawn have been created through the graphics property.
//for example:
var mymc:MovieClip = new MovieClip();
var g:Graphics = mymc.graphics;
g.moveTo(0,0);
g.lineTo(17.5,0);
g.lineTo(8.75,17.5);
g.lineTo(-8.75,17.5);
g.lineTo(0,0);
main code:
for each(var mc:MovieClip in impassable) {
//var bMatrix:Matrix = new Matrix();
//bMatrix.scale(mc.scaleX, mc.scaleY);
//bMatrix.rotate(mc.rotation * (Math.PI/180));
var bData:BitmapData = new BitmapData(mc.width, mc.height, true, 0);
//bData.draw(mc, bMatrix);
bData.draw(mc);
var bitmap:Bitmap = new Bitmap(bData);
bitmap.x = mc.x;
bitmap.y = mc.y;
var HitTest:Number = newCollision(bitmap, centerX, centerY, 13.7);
Any thoughts? thanks
This function will create a BitmapData clone of a DisplayObject, taking into account its transform matrix, though it doesn't take into account bitmap filters. (Based on this answer.)
function createBitmapClone(target:DisplayObject):BitmapData {
var targetTransform:Matrix = target.transform.concatenatedMatrix;
var targetGlobalBounds:Rectangle = target.getBounds(target.stage);
var targetGlobalPos:Point = target.localToGlobal(new Point());
// Calculate difference between target origin and top left.
var targetOriginOffset:Point = new Point(targetGlobalPos.x - targetGlobalBounds.left, targetGlobalPos.y - targetGlobalBounds.top);
// Move transform matrix so that top left of target will be at (0, 0).
targetTransform.tx = targetOriginOffset.x;
targetTransform.ty = targetOriginOffset.y;
var cloneData:BitmapData = new BitmapData(targetGlobalBounds.width, targetGlobalBounds.height, true, 0x00000000);
cloneData.draw(target, targetTransform);
return cloneData;
}
When you call successive transforms on a Matrix, the ordering is very important and can really mess things up.
Luckily there is a helper method that allows you to specify translation, rotation and scaling in one go and avoid those issues - createBox
For your case, something like this:
var matrix:Matrix = new Matrix();
matrix.createBox(mc.scaleX, mc.scaleY, mc.rotation*Math.PI/180, 0, 0);
(the two zeros are for x and y translation)

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 Many tweening problems

I am having huge problems with AS3 and the tweening class. It just stops for no apparent reason...
My code is a menu of side panels that slide in-and-out when the user clicks the tabs.
It looks as follows:
var mainContent1X:Tween = new Tween(MainContent1, "x", Strong.easeOut, MainContent1.x, 325, 1, true);
var MainContent2X:Tween = new Tween(MainContent2, "x", Strong.easeOut, MainContent2.x, 1750, 1, true);
var mainContent3X:Tween = new Tween(MainContent3, "x", Strong.easeOut, MainContent3.x, 1750, 1, true);
var MainContent4X:Tween = new Tween(MainContent4, "x", Strong.easeOut, MainContent4.x, 1750, 1, true);
var mainContent5X:Tween = new Tween(MainContent5, "x", Strong.easeOut, MainContent5.x, 1750, 1, true);
var MainContent6X:Tween = new Tween(MainContent6, "x", Strong.easeOut, MainContent6.x, 1750, 1, true);
It just stops for no reason at all like the code isn't being executed. Sometimes only one tween happens and the others don't. Sometimes it works the way I want it to!!! I can't figure out why this is happening, I am relatively new to AS3, I'll appreciate any help.
Regards
Luben
I assume its a lifetime problem of your variables. If your tween variables are only in local scope of a function, at the end of that function they are lost and so your Tweens.
Make them class variables or even better, use Tweener.
Thanks Daniel,
I declared the variables outside the function and all is fine. It seems that the garbage collector was clearing them at the wrong time. The code looks as follows now:
var mainContent1X:Tween;
function name() {
mainContent1X = new Tween(MainContent1, "x", Strong.easeOut, MainContent1.x, 325, 1, true);
...
}
I'll look into Tweener/TweenMax although I'm hesitant about using 3rd party software.
Thanks again,
Luben

Draw shadow without filter on Graphics object

I have a complex graphics data on a sprite or a shape (no matter which is exactly).
But I want to draw shadow (inner too) for one Rect ( [10, 10, 240, 25] for example) or another path.
/// code before
grObj.graphics.drawRect( 10, 10, 240, 25);
/// -> draw inner shadow on this objcect somehow
/// code after
Is it possible to perform w\o filters?
ps. http://ipicture.ru/uploads/100927/RHZF1K6Exu.png
solving:
BitmapData(*).applyFilter( *,*,*,*); // play with this function :)
also http://help.adobe.com/ru_RU/AS3LCR/Flash_10.0/flash/display/BitmapData.html#applyFilter()
i'm assuming you don't want to add filters to your object because you plan on adding a colorTransform and do not want the filters to also change color.
if that's the case what you can do is separate the filter by creating another sprite of the same size as the original, add a drop shadow filter to the new sprite with the hideObject parameter set to true.
//display object
var grObj:Sprite = new Sprite();
grObj.graphics.beginFill(0xFF0000, 1);
grObj.graphics.drawRect(0, 0, 240, 25);
grObj.graphics.endFill();
//filter object
var dsFilter:Sprite = new Sprite();
dsFilter.graphics.beginFill(0);
dsFilter.graphics.drawRect(0, 0, grObj.width, grObj.height);
dsFilter.graphics.endFill();
dsFilter.filters = [new DropShadowFilter(4.0, 45, 0, 1.0, 4.0, 4.0, 1.0, 3, true, false, true)];
//display list
grObj.x = dsFilter.x = 10;
grObj.y = dsFilter.y = 10;
addChild(grObj);
addChild(dsFilter);