creating movie clip instance using class in ActionScript3 - actionscript-3

First of all I want to say that I've just a couple hours experience with AS3.
Now, I have 2 .as files, one of them is main.as and the second one is Ship_.as
main.as :
package{
import flash .display.*;
// import Enemy;
public class main extends MovieClip{
public function main(){
var hero:Ship_=new Ship_();
addChild(hero); // I have to use addChild here also.
}
}
}
and Ship_.as is :
package{
import flash .display.*;
public class Ship_ extends MovieClip{
private var myHero:Ship=new Ship(); // moved inside of class definition
public function Ship_(){
addChild(myHero); // I think I added my movie clip into stage by this.
}
}
}
and here is my Ship MovieClip : http://prntscr.com/2pjzdwv
When I test it I get an error which says that "1013: The private attribute may be used only on class property definitions."
If I change private var myHero:Ship=new Ship(); to public var myHero:Ship=new Ship(); I get 1180: Call to a possibly undefined method addChild. error. Is there anyone to help me ?
EDIT
To be able to see space movieClip into screen, I have to use two addChild() method as I comment in the code,altough I thought the one inside Ship_ constructor would be enough for that.Could you explain why should I also use addChild(hero); ?

package{
import flash .display.*;
public class Ship_ extends MovieClip{
private var myHero:Ship=new Ship(); // moved inside of class definition
public function Ship_(){
addChild(myHero); // moved inside of contructor
}
}
}
When you call addChild() in Ship_, it adds myHero to the instance of Ship_. But where is Ship_'? At this point, it's just a variable inside of Main.main(), but it hasn't been added or anything.
So what you have is a lot like three boxes, A, B, and C. At first they're all laid out separtely, without one inside of another. But then you put Box C into Box B. But now you just have C inside of B, and A is still off by itself. So to complete the process, you have to put Box B into Box A, and since C is already inside of B, this will make it where C is ultimately inside of A as weel.

Related

How to add/remove movie clip using other .as file beside the main?

as the title says.
I have a practice.fla and on that have an Main.as file on it, I also have a movie clip that have an AS Linkage PlayButton on the library. I know how to display/remove it using the Main.as, now I have another .as file. Using the same technique done in Main.as, it doesn't work. Does anyone know how to do it?
Sorry if it is already been asked. I've tried searching the web but I can't pinpoint the exact.
You should be able to add movieclips as long as the class is derived from a Sprite or a MovieClip itself. Though keep in mind that you must add this new class to the stage for any children to be visible.
public class Main extends MovieClip {
public var other:Other;
public function Main() {
this.addChild(other = new Other);
}
}
public class Other extends Sprite {
public function Other() {
this.addChild(new Sprite());
}
}

AS3 - error 1120: Access of undefined property stage

I'm trying to add the QuickKong class to a game I'm making, as described here: http://www.kongregate.com/forums/90-kongregate-apis/topics/199155-quickkong-easy-kong-api-integration
To call the class, you use:
QuickKong.connectToKong(stage);
However, it keeps giving me:
error 1120: Access of undefined property stage.
Any suggestions? Thanks!
The stage is a property of a DisplayObject. When a DisplayObject is not on the Stage its stage property is undefined.
So, you need to make sure the stage is available when you run QuickKong.connectToKong(stage);.
If you do this in the constructor of your document class it should work just fine. Chances are you're trying to do this in some other class that doesn't have a stage property.
If the class you're trying to run this in extends a DisplayObject such as MovieClip or Sprite you can listen for when it is added to the stage and then run your QuickKong code. Like this:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Test extends MovieClip {
public function Test() {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(e:Event):void{
// now the stage is available
QuickKong.connectToKong(stage);
}
}
}
Now, if for some reason you are not running your QuickKong code in a class that has access to the stage you should pass a reference to the stage into that class's constructor, like this:
var someClass:SomeClass = new SomeClass(stage);
Lastly, in your document class you could make a static variable reference the stage. Like this:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
Main.stage = stage;
}
}
}
Now, you can just say: Main.stage anywhere in your code when you need to talk about the stage:
QuickKong.connectToKong(Main.stage);

inverse kinematic problems

I have a problem with inverse kinematic. I'm not able to move a tentacle and I don't know why. I implemented many simple skeletons and succeed, but in this case there must be something I'm missing. Hope you can help me!
Here you have a detailed explanation:
1.- This image displays the tentacle I want to move.
*"Flup" is a MovieClip containing both the tentacle and the circle below.
*The tentacle itself is another MovieClip (inside "Flup" as said before)
*The tentacle owns an armature
image
I just want to create a simple movement to start so, in this case, I will only try to move the blue bone.
2.- Flup is a class, so I create an instance in one frame
import IllGame.enemy.boss.*;
stage.addEventListener(MouseEvent.CLICK,moveTentacle);
//Creation of the monster and insertion into stage
var flup:Flup=new Flup();
flup.x=300;
flup.y=200;
stage.addChild(flup);
//Moves the tentacle (in this case the blue bone)
//to the click point
function moveTentacle(e:MouseEvent):void{
flup.moveArmature(mouseX,mouseY);
}
3.- Flup class, very simple at this moment
package IllGame.enemy.boss{
import flash.display.MovieClip;
public class Flup extends MovieClip{
public function Flup(){
}
//Recives the coordinates of the click point
//"TentacleOne" is the instance name of the tentacle
public function moveArmature(x:Number,y:Number){
this.tentacleOne.moves(x,y);
}
}
}
4.- Next, we have the tentacle class. This class is the father of every single tentacle Flup will have, so it's the father of "TentacleOne"
package IllGame.enemy.boss{
import flash.geom.Point;
import flash.display.MovieClip;
import fl.ik.*;
public class Tentacle extends MovieClip{
//Variables needed for inverse quinematic
private var armature:IKArmature;
private var bone:IKBone;
private var joint:IKJoint;
private var point:Point;
private var movement:IKMover;
private var armatureName:String;
private var bonesName:String;
private var bonesNumber:int;
public function Tentacle(armatureName:String,bonesName:String,bonesNumber:int){
this.armatureName=armatureName;
this.bonesName=bonesName;
this.bonesNumber=bonesNumber;
armature=IKManager.getArmatureByName(armatureName);
}
//This function is supposed to move the 7ยบ bone (the blue one)
public function moves(x:Number,y:Number){
bone=armature.getBoneByName(bonesName+"7");
joint=bone.headJoint;
movement=new IKMover(joint,joint.position);
movement.moveTo(new Point(x,y));
}
}
}
5.- Finally, TentacleOne class, which only sends information to its father
package IllGame.enemy.boss
{
public class FlupTentacleOne extends Tentacle{
const NUM_BONES:int=7;
public function FlupTentacleOne(){
super("Armature_20","ikBoneName",NUM_BONES);
}
}
}
I'm sorry for the long post, but I'm really annoyed about this and have no clue where the problem is. Thanks to everyone that tries to help me.
PS: When I run this code, X and Y vars for the skeleton position are modified but visually the armature keeps static.

Reference MovieClip After it is Added to Stage as a Child

I am currently having problems referencing a MovieClip child which I add to the Stage from the Document Class. Basically when the MovieClip child is added to the Stage from the Document Class, I want a certain MovieClip already on the Stage to reference it once it is on the Stage.
Also, if it is possible, I don't want the MovieClip referencing the child being added to the Stage to have parameters linking it with the Document Class, because I plan on nesting this MovieClip within another MovieClip later on in the future.
Here is the code for the MovieClip class which is referencing the child once it is added to the Stage:
package com.gameEngine.assetHolders
{
import com.gameEngine.documentClass.*;
import com.gameEngine.assetHolders.*;
import com.gameEngine.assetHolders.Levels.*;
import flash.display.*;
import flash.events.*;
public class FallingPlatform extends MovieClip
{
public var _document:Document;
// Trying to reference "_player"
public var _player:Player;
public var fallState:Boolean;
public var platformYSpeed:Number = 0;
public var platformGravityPower:Number = 0.75;
public function FallingPlatform()
{
this.addEventListener(Event.ADDED_TO_STAGE, initFallingPlatform);
// constructor code
}
public function initFallingPlatform(event:Event)
{
this.addEventListener(Event.ENTER_FRAME, dynamicFall);
this.addEventListener(Event.ENTER_FRAME, hitTest);
}
public function dynamicFall(event:Event)
{
if (this.fallState)
{
this.platformYSpeed += this.platformGravityPower;
y += this.platformYSpeed;
}
}
// Trying to reference "_player"
public function hitTest(event:Event)
{
if (this.hitTestPoint(_player.x, _player.y + 1, true))
{
this.fallState = true;
}
}
}
}
The player is initialized in the Document class, right? So for me, the best option is either passing the player reference in the constructor of your FallingPlatform class like this
public function FallingPlatform (thePlayer:Player) {
this._player = thePlayer
}
or having a setter method to pass it to it. In this way, you're not tying the structure of your code
public function set player (thePlayer:Player):void {
this._player = thePlayer
}
Hope it helps!
If you set a document class for a fla file every movieclip on the stage can be accessed by it's instance name - just as you wold create a var with its name.
Event more, you can do something like that:
If you place two movieclips on the stagefor example mc1 and mc2 you can add them as variables to the document class.
package{
public class DocClass{
public var mc1:MovieClip;
public var mc2:MovieClip;
[...]
}
}
and than you can access those movieclips from your class with code hints form your IDE (flash or flashbuilder)
the opposite is also availible: define variables in your class and than access them in flash
! it works best when your document class extends a Sprite, I haven;t tested it on extending from a MovieClip but it should also work

AS3 - passing the main timeline/stage into an external class

Suppose I have the following class
package {
import flash.display.Stage;
public class CustomObject {
private var stage:Stage;
public function CustomObject(stageRef:Stage) {
// stage access through
// constructor argument
stage = stageRef;
}
}
}
Which is not a document class. I wish to pass the stage of the main timeline into the class, say on frame 1
stop();
var c:CustomObject = new CustomObject(this.stage);
Is this the right way of passing the stage of the main timeline into another class?
That will work perfectly well - but if your custom class is extending a display object of any kind (Sprite, MovieClip, etc) it will have it's own stage property which is populated automatically if your object is in the display tree. I believe this would also mean your private variable would result in a compiler error.