PDA

View Full Version : Moving a character problem.



tic
November 29th, 2008, 05:12 PM
Hi,

I've made a character that I want to move from point A to point B.
Then when its at point B I want it to "teleport" back to point A. And go to point B again.

my code is

package {
//Importeer benodigde classes
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.text.TextField;
import flash.text.TextFormat;

public class Main extends Sprite {
public var myDuck:Eend;
public var myPduck:Eend;
public var Wing:Eend;
public var myCursor:Cursor;
public var yourScore=new Score ;
public var myScore=0;
public var theScore:TextField = new TextField();
public var myFormat:TextFormat = new TextFormat();
private var angle:Number=0;
private var range:Number=800;
private var speed:Number=0.03;

//Maak constructor functie
public function Main() {
init();
}
public function addScore(points) {
myScore+=points;
yourScore.Updatescore(myScore);
}

//Initialiseer de applicatie
private function init():void {

addChild(yourScore);

theScore.text = ("Punten");
theScore.x=700;
theScore.y=10;
theScore..selectable = false; ;
addChild(theScore);

myFormat.color=0xFF0000;
myFormat.size=24;
theScore.setTextFormat(myFormat);

//Groene eend
myDuck = new Eend();
myDuck.x=200;
myDuck.y=300;
myDuck.scaleX=0.4;
myDuck.scaleY=0.4;
myDuck.inkleuren("0x22CC22");
addChild(myDuck);

//Paarse eend
myPduck = new Eend();
myPduck.scaleX=-0.4;
myPduck.scaleY=0.4;
myPduck.x=600;
myPduck.y=300;
myPduck.inkleuren("0x800095");
addChild(myPduck);

//Cursor
myCursor = new Cursor();
stage.addEventListener(MouseEvent.MOUSE_MOVE , movecrs);
function movecrs(event:MouseEvent) {
myCursor.x=mouseX;
myCursor.y=mouseY;
myCursor.visible=true;
addChild(myCursor);
Mouse.hide();
myCursor.mouseEnabled=false;
}
addEventListener(Event.ENTER_FRAME, beweegBeest);

addEventListener(MouseEvent.MOUSE_DOWN, stopbeest);

}
public function beweegBeest(e:Event):void {
myPduck.x=Math.cos(angle)*range;
angle+=speed;

if (myPduck.x<-100) {
removeEventListener(Event.ENTER_FRAME, beweegBeest);
myPduck.x=650;
}
}
public function stopbeest(e:Event):void {
removeEventListener(Event.ENTER_FRAME, beweegBeest);
myPduck.x=650;

if (myPduck.x=650) {
addEventListener(Event.ENTER_FRAME, beweegBeest);
}
}
}
}Now the problem is.. When the char gets to point B, it "teleports" to point A like I want it to. But then the movent wont start again.. (I've got it to a point where it would go back to the -100 point and continu if clicked... so that wasn't a solution :/... )

Anyone know A way how i can get it to do what i want?

Btw, how do you post actionscripcode.. instead of code..

scottc
November 29th, 2008, 06:05 PM
if (myPduck.x<-100) {
removeEventListener(Event.ENTER_FRAME, beweegBeest);
myPduck.x=650;
}
Because you removed the event that moves the duck? (sorry, only had a quick glance at your code.)

and its the [ as ] tags to post actionscript.

tic
November 29th, 2008, 06:20 PM
Nah if i just make it jump withouth removing the event it still stops moving. (and i added the event later on btw)


if (myPduck.x=650) {
addEventListener(Event.ENTER_FRAME, beweegBeest);
}

scottc
November 29th, 2008, 06:38 PM
by the way.. why is it "greater then minus"? shouldn't it be <=?

if (myPduck.x<-100) {

tic
November 29th, 2008, 06:46 PM
Edit: ow i read your question wrong.

the -100 is the point i want the Duck to stop.

The final idee is have it come in screen.. then move out of the screen.. so thats why its in the minus. (and its a smaller then :))


Isnt the problem in that i use a ENTER_FRAME event? (don't really know what else i could use, just trying to help getting helpen :P )

I'm kinda new to as.. so. :P

scottc
November 29th, 2008, 07:09 PM
well you shouldn't try and assign variables inside the if() statement.



if(ducks = 1){ //assignment (this is bad.)
if(ducks == 2){ //comparison

Its bad because you will assign it to 1, and then it will always be true regardless of ducks original value.
(I used to do this, and never knew the difference lol.)



if(duck.x <= 100){ //if less then equal 100
duck.x = 500; //assign it to 500


ENTER_FRAME event is fine, either that or you can use a timer event aka "x" milliseconds instead of each frame render.

Edit:
If your a nut like me, you can use both. :D
Great for physics and stuff..

tic
November 29th, 2008, 07:20 PM
ok. But if I do this

package {
//Importeer benodigde classes
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.text.TextField;
import flash.text.TextFormat;

public class Main extends Sprite {
public var myDuck:Eend;
public var myPduck:Eend;
public var Wing:Eend;
public var myCursor:Cursor;
public var yourScore=new Score ;
public var myScore=0;
public var theScore:TextField = new TextField();
public var myFormat:TextFormat = new TextFormat();
private var angle:Number=0;
private var range:Number=800;
private var speed:Number=0.03;

//Maak constructor functie
public function Main() {
init();
}
public function addScore(points) {
myScore+=points;
yourScore.Updatescore(myScore);
}

//Initialiseer de applicatie
private function init():void {

addChild(yourScore);

theScore.text = ("Punten");
theScore.x=700;
theScore.y=10;
theScore..selectable = false; ;
addChild(theScore);

myFormat.color=0xFF0000;
myFormat.size=24;
theScore.setTextFormat(myFormat);

//Groene eend
myDuck = new Eend();
myDuck.x=200;
myDuck.y=300;
myDuck.scaleX=0.4;
myDuck.scaleY=0.4;
myDuck.inkleuren("0x22CC22");
addChild(myDuck);

//Paarse eend
myPduck = new Eend();
myPduck.scaleX=-0.4;
myPduck.scaleY=0.4;
myPduck.x=600;
myPduck.y=300;
myPduck.inkleuren("0x800095");
addChild(myPduck);

//Cursor
myCursor = new Cursor();
stage.addEventListener(MouseEvent.MOUSE_MOVE , movecrs);
function movecrs(event:MouseEvent) {
myCursor.x=mouseX;
myCursor.y=mouseY;
myCursor.visible=true;
addChild(myCursor);
Mouse.hide();
myCursor.mouseEnabled=false;
}
addEventListener(Event.ENTER_FRAME, beweegBeest);



}
public function beweegBeest(e:Event):void {
myPduck.x=Math.cos(angle)*range;
angle+=speed;

if (myPduck.x<=100) {
myPduck.x=500;
}
}
}
}



The duck stops moving at the moment it goes to x 500. And as you can see. There is no remove event in my code..


Edit: ow lol... if I wait a while it now also goes back... so it jumps to 500 like it should..

Then nothing happends for about 3 seconds.. en then it jumps back to x100 and goes backwords to the start point.. :s

scottc
November 29th, 2008, 07:39 PM
myPduck.x=Math.cos(angle)*range;
angle+=speed;

if (myPduck.x<=100) {
myPduck.x=500;
}
Maybe you should reset angle back to 0.
Because angle is getting bigger and bigger, then once it hits your x value, the duck will stop... but angle will keep going around until 360... then the duck will start again from 361... with the x back at 500.

You can probably even forget the x value, and just monitor the angle variable.

Edit:


private function init():void {
//....etc....
function movecrs(event:MouseEvent) {
}
}

//place movecrs here instead

also avoid nesting functions.

tic
November 29th, 2008, 07:43 PM
Jup thats it.. Thanx again :)