inverse kinematic problems - actionscript-3

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.

Related

how do you reference a symbol in a different class in as3?

I have created two separate classes and I want to use a symbol I created in the main class in a function I created in the second class. I have tried importing both classes into each other, however when I do this I get Error #1023. I am fairly new at as3 and any help is appreciated as I have no idea what I have done wrong.
-Thank you!
public class SuspectSimulatorDesktop extends Sprite {
[Embed(source="/../lib/SuspectSit.png")]
private var CharacterSit:Class;
var tools:Tools = new Tools();
public var charSit:Bitmap = new CharacterSit();
public function SuspectSimulatorDesktop() {
addChild(tools);
}
}
//Tools (Second Class)
package com.powerflasher.SampleApp {
import com.powerflasher.SampleApp.SuspectSimulatorDesktop;
import flash.events.MouseEvent;
import flash.display.Sprite;
/**
* #author timcis
*/
public class Tools extends Sprite {
[Embed(source="/../lib/Fist.png")]
private var Fist:Class;
var sSim:SuspectSimulatorDesktop = new SuspectSimulatorDesktop();
private function punchChar(event:MouseEvent):void{
sSim.charSit.rotation = 90;
}
}
Error #1023 means that you have a stack overflow in your code. See this link for more explanations :
http://curtismorley.com/2007/08/19/flashflex-as3-error-1023-stack-overflow-occurred/
Regarding your code, you created a SuspectSimulatorDesktop class which instantiates a Tools object which instantiates itself a SuspectSimulatorDesktop object and so on... Each class calls the other one indefinitely and fills the stack, hence the stack overflow.
You need to break the circle by deleting either of these lines and adapt your code accordingly :
var tools:Tools = new Tools();
or
var sSim:SuspectSimulatorDesktop = new SuspectSimulatorDesktop();

creating movie clip instance using class in ActionScript3

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.

Actionscript 3.0 method keeps repeating, cant figure out why

I'm having difficulty debugging my audio slider. I'm pretty sure my problems lies in the fact that one of my methods, changeVolumeRedFireball is just constantly repeating at a very fast rate. I get a glitchy sound every once in a while in my game, so it seems to correlate. I traced "output" inside the method and quickly found out it's repeating at a high rate.
Problem is, I cannot figure out WHERE this is coming from! One other note. This only starts repeating once I hold down my slider, hence activating the changeVolumeRedFireball from dragSliderRedFireball
I do have other methods from other classes referencing methods in this class. They only access playSoundRedFireball and stopSoundRedFireball though, so I don't see why that would have any effect. Also, this class is instantiated by my document class upon start up of the game. I suppose I'll put in the relevant code from the document class if requested, but I just didn't think it would affect this problem at all.
package {
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.geom.Rectangle;
public class VolumeRedFireball extends Sprite {
public var redFireballSnd:Sound = new Sound();
public var redFireballChannel:SoundChannel = new SoundChannel();
//URLRequest=new URLRequest("solitude.wav");
//Make sure you pass URLRequest an audio file on your computer.
public var reqRedFireball:EnemyAppearSound = new EnemyAppearSound();
public var boundaryRedFireball:Rectangle;
public var spriteRedFireball:Sprite;
public var sliderRedFireball:Sprite;
public var xPosRedFireball:Number;
public var yPosRedFireball:Number;
public static var volRedFireball:Number = 1;
public function VolumeRedFireball() {
this.addEventListener(Event.ADDED_TO_STAGE, onStageRedFireball,false,0,true);
volRedFireball=1;
redFireballChannel.soundTransform=new SoundTransform(volRedFireball)
}
public function onStageRedFireball(e:Event):void
{
//We remove it immediately so that it doesn't get called multiple times
//As the instance is added to the display list tree
this.removeEventListener(Event.ADDED_TO_STAGE, onStageRedFireball);
xPosRedFireball = 320;
yPosRedFireball = 170;
initRedFireball();
}
public function initRedFireball():void {
spriteRedFireball = new Sprite();
redFireballChannel.stop();
spriteRedFireball.graphics.beginFill(0x999999);
spriteRedFireball.graphics.drawRect(xPosRedFireball,yPosRedFireball,100,5);
spriteRedFireball.graphics.endFill();
addChild(spriteRedFireball);
spriteRedFireball.x-=spriteRedFireball.width/2;
sliderRedFireball = new Sprite();
sliderRedFireball.graphics.beginFill(0xFF0000);
sliderRedFireball.graphics.drawCircle(xPosRedFireball+50,yPosRedFireball, 15);
sliderRedFireball.graphics.endFill();
addChild(sliderRedFireball);
sliderRedFireball.addEventListener(MouseEvent.MOUSE_DOWN, dragsliderRedFireball);
stage.addEventListener(MouseEvent.MOUSE_UP, stopsliderRedFireball);
boundaryRedFireball=new Rectangle(-100,0,100,0);
}
public function dragsliderRedFireball(event:MouseEvent):void {
sliderRedFireball.startDrag(false,boundaryRedFireball);
sliderRedFireball.removeEventListener(MouseEvent.CLICK, dragsliderRedFireball);
sliderRedFireball.addEventListener(Event.ENTER_FRAME, changeVolumeRedFireball);
}
public function stopsliderRedFireball(event:MouseEvent):void {
sliderRedFireball.stopDrag();
sliderRedFireball.removeEventListener(MouseEvent.MOUSE_UP, stopsliderRedFireball);
}
public function changeVolumeRedFireball(event:Event):void {
volRedFireball=1+Math.round(sliderRedFireball.x)/100;
redFireballChannel.soundTransform=new SoundTransform(volRedFireball);
trace("output");
}
public function playSoundRedFireball():void
{
redFireballChannel = reqRedFireball.play();
}
public function stopSoundRedFireball():void
{
redFireballChannel.stop();
}
}
}
Changing a SoundTransform during every frame isn't good, as you are essentially undermining the audio channel. It's better if you use MouseEvent.MOUSE_MOVE to trigger volume change, as if mouse is moved, and volume slider is being dragged, then the SWF user apparently wants the volume to change. But if a user starts dragging the slider but does not move it, why changing the volume?
public function dragsliderRedFireball(event:MouseEvent):void {
sliderRedFireball.startDrag(false,boundaryRedFireball);
sliderRedFireball.removeEventListener(MouseEvent.MOUSE_DOWN, dragsliderRedFireball);
sliderRedFireball.addEventListener(MouseEvent.MOUSE_MOVE, changeVolumeRedFireball);
sliderRedFireball.removeEventListener(MouseEvent.MOUSE_UP, stopsliderRedFireball);
}
public function stopsliderRedFireball(event:MouseEvent):void {
sliderRedFireball.stopDrag();
sliderRedFireball.removeEventListener(MouseEvent.MOUSE_UP, stopsliderRedFireball);
sliderRedFireball.removeEventListener(MouseEvent.MOUSE_MOVE, changeVolumeRedFireball);
sliderRedFireball.addEventListener(MouseEvent.MOUSE_DOWN, dragsliderRedFireball);
}
Also, you have messed up your listeners. First, you are not removing the enterframe listener after you stop dragging the fireball. Second, you are not adding a start-drag listener back after the fireball has been released. And third, in your initRedFireball you are adding stopsliderRedFireball as listener to stage, for a really strange reason, but you are attempting to remove it from sliderRedFireball. Please pay precise attention on where your listeners go, what do they listen and where do you remove them and from which objects. Misuse of an enterframe listener can build up pretty quickly, and spoil you all the fun.

AS 3.0: Calling a method, which is defined in another class, from Main class gives Error 1120

I have two classes. The Main class calls a function, which is defined in a Second class. I'm getting the following error:
Error 1120: Access of undefined property myFunction
Basically, I am creating buttons in the Main class that will add a corresponding Child to an Object in the Second class (if you click one button, child x1 will be added, if you click another button, child x2 will be added, and so forth).
Here's the relevant code for the Main.as file:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip {
private var x1:X1 = new X1();
private var x2:X2 = new X2();
public function Main():void {
addPlayers();
}
public function addPlayers():void {
addChild(x1);
addChild(x2);
x1.x=325;
x1.y=5;
x2.x=366;
x2.y=5;
x1.label = "dog";
x2.label = "cat";
x1.addEventListener(MouseEvent.CLICK, selectPlayer);
x2.addEventListener(MouseEvent.CLICK, selectPlayer);
}
}
}
The Second.as file code is:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
public class Second extends MovieClip
{
public var myVar:MyVar = new MyVar();
public function Second():void
{
addChild(myVar);
}
private var mc_x1:Mc_x1 = new Mc_x1();
private var mc_x2:Mc_x2 = new Mc_x2();
public function selectPlayer(event:MouseEvent):void
{
if (Game(this.parent).turn == 0) {
myVar.addChild(mc_x1);
} else {
switch (event.currentTarget.label) {
case "dog":
myVar.addChild(mc_x1);
break;
case "cat":
myVar.addChild(mc_x2);
break;
default:
myVar.addChild(mc_x1);
}
}
}
}
}
i've tried defining a new variable as a public static var and that hasn't worked. i've also tried to import the Second class in the Main class and that didn't work either. any thoughts?
thank you!
If I'm understanding what your code says, you can't do what your doing. Even though you are listening to events on X1 and X2, you are listening to them FROM main. In other words, main is attempting to handle the event, and it's not finding the function you specified (selectPlayer). If you want all event handling to happen in main, then main will have to call into X1 and X2 when it receives an event.
I agree with ThatSteveGuy , in Main you are calling a function that doesn't exist, namely selectPlayer(). Following your code the first thing you should do is add a selectPlayer() function in Main.
Then you would need to add an instance of the Second class in Main in order for the following to work
private function selectPlayer(event:MouseEvent):void
{
second.selectPlayer(event);
}
Now it's a bit difficult to advise you any further because this way of doing things looks a bit convoluted but then again I would need to see the big picture. This is just an explanation about why your code doesn't work. Also, in the Second class , selectPlayer may throw an error because Game(this.parent ) will return a null value so you won't be able to access the turn property...
Is there a good reason why you need to add the two buttons in Main as opposed to adding them in Second?
try this
public function Second():void
{
this.addEventListener(MouseEvent.CLICK, selectPlayer);
addChild(myVar);
}
(and don't forget to remove x1.addEventListener, x2.addEventListener from main)

Constructor arguments problem ActionScript 3

I have a custom class defined in Actionscript and I want to make an instance of it in the main document of Flash application. However, after calling the constructor with one argument, Flash gives me this error:
Error #1063: Argument count mismatch on coa.application::MenuItem(). Expected 1, got 0.
This is my class:
public class MenuItem extends MovieClip{
var button:SimpleButton;
public function MenuItem(buttonLoc:uint) {
button = new InvBtn();
this.addChild(button);
button.x=-81;
button.y=buttonLoc*33;
button.addEventListener(MouseEvent.CLICK, mybringToFront);
}
}
And this is my attempt to call its constructor:
var menu1:MovieClip = new MenuItem(3);
Any idea, whats wrong?
Apologies, I can't comment yet, or I'd put this in a comment.
Are you sure that:
var menu1:MovieClip = new MenuItem(3);
is the only place that you're constructing a new MenuItem? You don't by any chance have the MenuItem class attached to some instances on the stage?
I changed your code to this (just so I could run it) and it works fine:
package{
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
public class MenuItem extends MovieClip{
var button:SimpleButton;
public function MenuItem(buttonLoc:uint) {
button = new SimpleButton();
this.addChild(button);
button.x=-81;
button.y=buttonLoc*33;
button.addEventListener(MouseEvent.CLICK, mybringToFront);
}
public function mybringToFront(event:MouseEvent):void{
trace('blah');
}
}
}
Like quoo said, most likely you have an instance of the object that the class is attached to on stage. To test for that do this:
public class MenuItem extends MovieClip{
var button:SimpleButton;
// I changed it to int, cuz uint is extremely slow for any math
// other than bitwise operators, int is fast as long as no fractions
public function MenuItem(buttonLoc:int = -1) {
if (buttonLoc == -1)
trace("On stage instance found! Location: "+x+", "+y);
button = new InvBtn();
this.addChild(button);
button.x=-81;
button.y=buttonLoc*33;
button.addEventListener(MouseEvent.CLICK, mybringToFront);
}
}