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();
Related
How to create chipmunk debug layer with Cocos2d-JS v3?
I could not find an example of how to do it.
Assuming you have added "chipmunk" to "modules"in your projects project.json, simply place the following within the ctor or init method of the Layer that has the Chipmunk space defined in it:
//Add the Chipmunk Physics space
var space = new cp.Space();
space.gravity = cp.v(0, -10);
//Add the Debug Layer:
var debugNode = new cc.PhysicsDebugNode(space);
debugNode.visible = true;
this.addChild(debugNode);
You could also add the following to set up a "floor" and a sprite to bounce on it:
//add a floor:
var floor = new cp.SegmentShape(this.space.staticBody, cp.v(-1000, 10), cp.v(1000, 0), 10);
floor.setElasticity(1);
floor.setFriction(0);
space.addStaticShape(floor);
//add a square to bounce
var myBody = new cp.Body(Infinity, cp.momentForBox(Infinity, 10, 50));
myBody.p = cc.p(derecha - 10, arriba / 2);
space.addBody(myBody);
var myShape = new cp.BoxShape(myBody, 10, 50);
myShape.setElasticity(1);
myShape.setFriction(0);
space.addShape(myShape);
To do this, i must add the module "physics" in project.json and then use cc.PhysicsDebugNode
I have an instance of a MovieClip called 'BlueBox', which is just a 'Drawing Object' which is blue. It is inside a container called 'option1MC' which is inside 'option1Container' which is on the stage.. It's color can be changed like so:
option1Container.addEventListener(MouseEvent.MOUSE_OVER, option1ContainerOver);
var optionOver:ColorTransform = new ColorTransform();
optionOver.color = 0xC56516;
function option1ContainerOver(evt:Event):void {
option1Container.option1MC.BlueBox.transform.colorTransform = optionOver;
}
I want to change the color to a gradient. This is what I tried:
var mtr:Matrix = new Matrix();
mtr.createGradientBox(option1Container.option1MC.BlueBox.width, option1Container.option1MC.BlueBox.height, 0, 0, 0);
mtr.graphics.beginGradientFill(GradientType.LINEAR, [0xFF0000, 0x0000FF], [1, 1], [0x00, 0xFF], mtr);
mtr.graphics.drawRect(0,0,option1Container.option1MC.BlueBox.width, option1Container.option1MC.BlueBox.height);
However, right now when I run the .fla file, it i giving me an error saying:
Access of possible undefined property graphics through a reference with static type flasg.geom:Matrix:
and is referring to the lines
mtr.graphics.beginGradientFill(GradientType.LINEAR, [0xFF0000, 0x0000FF], [1, 1], [0x00, 0xFF], mtr);
mtr.graphics.drawRect(0,0,option1Container.option1MC.BlueBox.width, option1Container.option1MC.BlueBox.height);
Any idea why?
A Matrix does not have a graphics property. You would need to apply this to a DisplayObject with a graphics property. That would look something like this with your setup:
//create the sprite to do the gradient fill on or use your MovieClip
//var spriteObject:Sprite = new Sprite() or just use option1Container;
//enter your matrix
var mtr:Matrix = new Matrix();
mtr.createGradientBox(option1Container.BlueBox.width, option1Container.BlueBox.height, 0, 0, 0);
//apply it to the sprite
sprite.graphics.beginGradientFill("linear", [0xFF0000, 0x0000FF], [1, 1], [0x00, 0xFF], mtr);
sprite.graphics.drawRect(0, 0, 50, 40);
sprite.graphics.endFill();
addChild(sprite);
I created a sprite variable here, which is not necessary as you can just swap sprite with option1Container
You can reference adobe's help for this as well: http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7dd7.html
Now if you drew the MovieClip graphics in the IDE you won't currently be able to change it. In which case you can create a new MovieClip and add it to your container like so:
//note I made a target variable just to save on typing:
var target:MovieClip = option1Container.option1MC.BlueBox;
var gradientLayer:MovieClip = new MovieClip();
var gradientBox:Matrix = new Matrix();
gradientBox.createGradientBox(target.width, target.height, 0, 0, 0);
gradientLayer.graphics.beginGradientFill("linear", [0xFF0000, 0x0000FF], [1, 1], [0x00, 0xFF], gradientBox);
gradientLayer.graphics.drawRect(0, 0, target.width, target.height);
gradientLayer.graphics.endFill();
//giving the asset a name will allow you to easily remove it later
gradientLayer.name = "gradientLayer";
target.addChild(gradientLayer);
Notice I gave the gradient asset a name of "gradientLayer". If you want to remove this child later on you can do that with:
option1Container.option1MC.BlueBox.removeChild( option1Container.option1MC.BlueBox.getChildByName( "gradientLayer" ) );
I cannot print the stage:
btn.addEventListener(MouseEvent.CLICK, printFunction);
function printFunction(event:MouseEvent):void
{
var myPrintJob:PrintJob = new PrintJob();
myPrintJob.addPage(0);
myPrintJob.send();
}
it gives me a compile error:
1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:Sprite.
I've also tried:
myPrintJob.addPage(Sprite(0));
there's no compile error; but when I click the print button, there's no print dialog and the output section in Flash gives me this error:
TypeError: Error #1034: Type Coercion failed: cannot convert 0 to flash.display.Sprite.
at Untitled_fla::MainTimeline/printFunction()
Printjob's addPage method is expecting a Sprite as first parameter.
What are you trying to achive by passing 0?
If you want a blank page, try:
var myPrintJob:PrintJob = new PrintJob();
myPrintJob.start(); /*Initiates the printing process for the operating system, calling the print dialog box for the user, and populates the read-only properties of the print job.*/
myPrintJob.addPage( new Sprite() );
myPrintJob.send();
Another example with a red square:
var s:Sprite = new Sprite();
s.graphics.beginFill(0xFF0000);
s.graphics.drawRect(0, 0, 80, 80);
s.graphics.endFill();
var myPrintJob:PrintJob = new PrintJob();
myPrintJob.start(); /*Initiates the printing process for the operating system, calling the print dialog box for the user, and populates the read-only properties of the print job.*/
myPrintJob.addPage( s );
myPrintJob.send();
More info here.
To print a part of stage, you could:
1) Wrap everything you want to print in a sprite and pass that sprite to addPage().
or
2) Use BitmapData
var bd :BitmapData = new BitmapData(stage.width, stage.height, false);
bd.draw(stage);
var b:Bitmap = new Bitmap (bd);
var s:Sprite = new Sprite();
s.addChild(b);
var printArea = new Rectangle( 0, 0, 200, 200 ); // The area you want to crop
myPrintJob.addPage( s, printArea );
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);
The below is my code for trying to clone the MovieClip and it doesn't work.
We should see two cirles if the codes is working correctly.
/*The original MovieClip*/
var circle:MovieClip = new MovieClip();
circle.graphics.beginFill(0xAA0022);
circle.graphics.drawCircle(40, 40, 40);
circle.x=10
addChild(circle);
/*CLONE the MovieClip - IT DOES'T WORK FROM HERE DOWN*/
var cloneCirle:MovieClip = new MovieClip();
cloneCirle=circle
cloneCirle.x=60
addChild(cloneCirle);
When you do cloneCircle=circle, it's not copying or cloning anything. It's just saying that the variable cloneCircle is another name for your original circle MovieClip. What you need to do is use the Graphics.copyFrom() method.
Try it:
var cloneCircle:MovieClip = new MovieClip();
cloneCircle.graphics.copyFrom(circle.graphics);
cloneCircle.x = 60;
addChild(cloneCircle);
This is for creating a duplicate of a stage object that exists in the FLA library at compile time
The object must have a 'Export for Actionscript ticked in it's properties panel and a valid class name in the 'Class' box
If the symbol only has a single frame just add another so it registers as MovieClip() rather than Sprite()
private function cloneObject(source:DisplayObject):void
{
var objectClass:Class = Object(source).constructor;
var instance:MovieClip = new objectClass() as MovieClip;
instance.transform = source.transform;
instance.filters = source.filters;
instance.cacheAsBitmap = source.cacheAsBitmap;
instance.opaqueBackground = source.opaqueBackground;
source.parent.addChild(instance);
instance.x += 20; // just to show the duplicate exists!
}
http://snipplr.com/view/44734/
Adapted from here:
function copyClip( clip:MovieClip )
{
var sourceClass:Class = Object(clip).constructor;
var duplicate:MovieClip = new sourceClass();
return duplicate;
}