I’ve fairly new to flash’s class system, and I’ve been messing around with it for a couple days and what i’ve been doing is making a little ball physics thing, and It works for me (the collision and physics), but when I do the Input for the player, it does it to both the test ball (one that the player can bump into to test collisions), and the player… I’ve tried everything I could to fix this, but I’m stumped, can anyone help? Here’s my code for the Ball class:
class Ball {
var x:Number;
var y:Number;
var Obj;
var maxSpeed = 8;
var ball:Boolean = true;
var oldPos:Object = {x:0,y:0};
var vel:Object = {x:0,y:0};
var acc:Object = {x:0,y:0};
var fricCoef:Number = .1;
function Ball() {
Obj = this;
}
function ControlPhysics():Void {
var fric = {x:-Obj.vel.x,y:-Obj.vel.y};
Obj.acc.x += (fric.x*Obj.fricCoef);
Obj.acc.y += (fric.y*Obj.fricCoef);
Obj.vel.x += Obj.acc.x;
Obj.vel.y += Obj.acc.y;
if(Obj.vel.x >= -0.1 && Obj.vel.x <= 0.1) Obj.vel.x = 0;
if(Obj.vel.y >= -0.1 && Obj.vel.y <= 0.1) Obj.vel.y = 0;
oldPos = {x:Obj._x,y:Obj._y};
Obj._x += Obj.vel.x;
Obj._y += Obj.vel.y;
Obj.acc.x = Obj.acc.y = 0;
}
function ControlCollisions(obj):Void {
if(Dist(obj._x,obj._y,Obj._x,Obj._y) <= (Obj._width/2)+(obj.ball?(obj._width/2):0)) {
Obj._x = oldPos.x;
Obj._y = oldPos.y;
var oldMag = Dist(0,0,vel.x,vel.y);
var collision = {x:obj._x-Obj._x,y:obj._y-Obj._y};
Normalize(vel);
Normalize(collision);
var tempDotProd = DotProduct(vel,collision);
vel.x -= 2*tempDotProd*collision.x;
vel.y -= 2*tempDotProd*collision.y;
var currentMag = Dist(0,0,vel.x,vel.y);
if(obj.ball) {
var newObjMag = (((obj._width/2)*1.25)/((Obj._width/2)*1.25))*(oldMag/2);
var newobjMag = (((Obj._width/2)*1.25)/((obj._width/2)*1.25))*(oldMag/2);
ScaleVector(vel,currentMag,newObjMag);
obj.vel.x += 2*tempDotProd*collision.x;
obj.vel.y += 2*tempDotProd*collision.y;
var currentMag = Dist(0,0,obj.vel.x,obj.vel.y);
ScaleVector(obj.vel,currentMag,newobjMag);
}else {
ScaleVector(vel,currentMag,oldMag);
}
}
}
function Dist(x1:Number,y1:Number,x2:Number,y2:Number):Number {
return Math.sqrt(Dist2(x1,y1,x2,y2));
}
function Dist2(x1:Number,y1:Number,x2:Number,y2:Number):Number {
return Math.pow(x1-x2,2)+Math.pow(y1-y2,2);
}
function Normalize(vec:Object):Void {
var d = Dist(0,0,vec.x,vec.y);
vec.x = vec.x/d;
vec.y = vec.y/d;
}
function ScaleVector(vec:Object,oMag:Number,nMag:Number):Void {
var Change = nMag/oMag;
vec.x = vec.x*Change;
vec.y = vec.y*Change;
}
function DotProduct(vec1:Object,vec2:Object):Number {
return (vec1.x*vec2.x)+(vec1.y*vec2.y);
}
};
And for my Hero class:
class Hero extends Ball {
function Hero() {
}
public function onEnterFrame():Void {
ControlKeys();
ControlPhysics();
CheckCollision(_root.bcont);
}
function CheckCollision(cont:MovieClip):Void {
for(var objs in cont) {
var o = cont[objs];
if(o != Obj) {
ControlCollisions(o);
}
}
}
function ControlKeys():Void {
Key.isDown(Key.LEFT) ? (Obj.acc.x > -Obj.maxSpeed ? Obj.acc.x -= .5 : Obj.acc.x = -Obj.maxSpeed) : (Key.isDown(Key.RIGHT) ? (Obj.acc.x < Obj.maxSpeed ? Obj.acc.x += .5 : Obj.acc.x = Obj.maxSpeed) : null);
Key.isDown(Key.DOWN) ? (Obj.acc.y > -Obj.maxSpeed ? Obj.acc.y += .5 : Obj.acc.y = -Obj.maxSpeed) : (Key.isDown(Key.UP) ? (Obj.acc.y < Obj.maxSpeed ? Obj.acc.y -= .5 : Obj.acc.y = Obj.maxSpeed) : null);
}
};
And my base enemy (the test object):
class BaseEnemy extends Ball {
function BaseEnemy() {
}
private function onEnterFrame():Void {
ControlPhysics();
}
};
And on frame 1 in the main timeline:
_root.createEmptyMovieClip("bcont",99);
bcont.attachMovie("Hero","Hero",1,{_x:random(Stage.width),_y:random(Stage.height)});
bcont.attachMovie("Ball","Ball",2,{_x:random(Stage.width),_y:random(Stage.height)});
If anyone can help, i’d really appreciate it