Hiya
I have this program detecting a collision and calling a function called changeDirection.
In here I need to check which way the character is currently moving and change it to the opposite.
I will attach RollableChar.as It will be missing few functions but these are not needed for this problem.
I have some code in this function but it wont work.
I would appreciate any help possible.
Thank you very much in advance.
Laura
package classes
{
import classes.MainApp2;
import caurina.transitions.Tweener;
import fl.motion.ITween;
import flash.display.*;
import flash.events.*;
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.utils.getTimer;
import flash.utils.setTimeout;
public class RollableChar extends MovieClip
{
private var mousePoint:Number; // x position of mouse
private var leftWall:Number; //left limit
private var rightWall:Number; //right limit
private var topHeightLimit:Number; //top Y limit
private var bottomHeightLimit:Number; // bottom Y limit
private var step:Number; //speed the object moves
private var bounceIn:Boolean; // Check to if true to bounceOnScreen
private var mouseFinder:Boolean; // Check if true to find mouse position otherwise roll right
private var delay:Number; // miliseconds to timeout
private var speed:Number; // speed the rolly will move
private var range:Number; // distnace from position of mouse
private var scaleRangeUpper:Number; // scale the maximum size of rolly
private var scaleRangeLower:Number; // scale the minimum size of rolly
private var upper:Number; // get a random scale between max and min
private var direction:int; //should only be 1 or -1
public function RollableChar():void
{
//matchScaleToSpeed();
//matchScaleToYPosition ();
leftWall = 5;
rightWall = 800;
topHeightLimit = 100;
bottomHeightLimit = 20;
step = 10;
bounceIn = true;
mouseFinder = true;
delay = 400;
speed = Math.random() +1.1;
range = 80;
scaleRangeUpper = 1;
scaleRangeLower = .3;
upper = (scaleRangeUpper - scaleRangeLower) * 10;
direction = 1;
}
public function start()
{
trace (name + ".start()");
matchScaleToSpeed();
matchScaleToYPosition();
positionOffScreen();
//bounceOnScreen();
if (bounceIn) bounceOnScreen();
else rollLeft();
addEventListener(Event.ENTER_FRAME, checkForCollisions);
}
private function checkForCollisions(event:Event) {
if (detectCollision()) changeDirection();
}
private function detectCollision():Boolean
{
var success:Boolean = false;
//trace ("parent: "+(parent as MainApp2).rollyArray.length);
//get array off the mainapp
var rollyArray:Array = (parent as MainApp2).rollyArray;
//loop
for each (var otherRolly:RollableChar in rollyArray) {
if (otherRolly == this) continue; //skip onto next loop
if (this.hitTestObject(otherRolly)) return true;
//trace("Hit between:" + this && otherRolly)
}
return success;
}
private function changeDirection()
{
//setDirection();
//Tweener.pauseTweens(this, "_x");
//Tweener.pauseTweens(this); // pause the tween
// check direction - make opp
//if (direction ==1) this.direction *= -1;
//if (direction == -1) this.direction *= -1;
//if (rollLeft()) rollRight();
//if (rollRight()) rollLeft();
//start the opposite tween
//Tweener.resumeAllTweens();
//Tweener.resumeTweens(this, "_x");
}
private function positionAfterBounce():void
{
y = topHeightLimit + Math.random() * (bottomHeightLimit - topHeightLimit);
}
private function findMouse():void
{
mousePoint = this.stage.mouseX;
mousePoint = Math.round (mousePoint + (Math.random() * range) - range/2);
trace (mousePoint);
Tweener.addTween(this, { x:mousePoint, time:speed, transition:"easeInOutCubic", onComplete:mouseFound } );
}
private function mouseFound():void
{
setTimeout(findMouse, delay);
}
private function positionOffScreen()
{
x = leftWall + Math.random() * (rightWall - leftWall);
y = -100;
}
private function bounceOnScreen()
{
var destination:Number = 310;
Tweener.addTween(this, { y:destination, time:speed, transition:"easeOutBounce", onComplete:bounceInFinished } );
}
private function bounceInFinished()
{
rollRight();
//findMouse();
}
private function rollRight()
{
var destination:Number = 830;
Tweener.addTween(this, {x:destination, time:speed, transition:"easeInOutCubic", onComplete:rollRightFinished} );
}
private function rollRightFinished()
{
rollLeft();
}
private function rollLeft()
{
var destination:Number = 10;
Tweener.addTween(this, { x:destination, time:speed, transition:"easeInOutCubic", onComplete:rollLeftFinished } );
}
private function rollLeftFinished()
{
if (mouseFinder) findMouse();
else rollRight();
}
}
}