There has to be a better way to do this, but I’ve been banging my head against the wall for four days now.
I have a little game going where a movieclip (star) bounces off of some obstacles (cloud). The clouds are generated using a loop. When the star hits a cloud, the cloudMC advances to the next frame and then removes itself.
The problem is I have to declare a hittest for each cloud.
Anyone have any idea how I can use the array variable to detect which cloud is being hit?
Thanks in advance for any suggestions
See code below:
//CLOUD MAKER
floatcloud = function () {
clouds = 20;
// quantity
for (var i=0; i<clouds; i++) {
q = attachMovie("cloudMC", "cloud"+i, i);
q._x = random(600);
q._y = random(900);
q.drop = .75;
q.wind = -0.5+Math.random()*(0.4*3);
q.onEnterFrame = cloudmover;
}
};
floatcloud();
//STAR PHYSICS
_root.attachMovie(“star”, “starMC”, 10000,{_x:300,_y:100});
power = 0.25;
yspeed = 0.0;
xspeed = 0.0;
wind = 0.00;
gravity = 0.10;
upconstant = 0.25;
friction = .99;
starMC.onEnterFrame = function() {
//STAR START MOTION
xspeed = (xspeed+wind)*friction;
yspeed = yspeed+gravity;
if (xspeed>15) {
xspeed = 15;
}
if (xspeed<-15) {
xspeed = -15;
}
if (yspeed>15) {
yspeed = 15;
}
if (yspeed<-15) {
yspeed = -15;
}
this._y += yspeed;
this._x += xspeed;
//STAR CONTROLS
if (Key.isDown(Key.LEFT)) {
xspeed = xspeed-power;
}
if (Key.isDown(Key.RIGHT)) {
xspeed = xspeed+power;
}
if (Key.isDown(Key.UP)) {
yspeed = yspeed-powerupconstant;
}
if (Key.isDown(Key.DOWN)) {
yspeed = yspeed+powerupconstant;
}
//cloud hit
if (this.hitTest(cloud0.hitArea)){
yspeed =-1 * yspeed;
cloud0.gotoAndPlay(2);
//removeMovieClip(cloud1);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud1.hitArea)){
yspeed =-1 * yspeed;
cloud1.gotoAndPlay(2);
//removeMovieClip(cloud1);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud2.hitArea)){
yspeed =-1 * yspeed;
cloud2.gotoAndPlay(2);
//removeMovieClip(cloud2);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud3.hitArea)){
yspeed =-1 * yspeed;
cloud3.gotoAndPlay(2);
//removeMovieClip(cloud3);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud4.hitArea)){
yspeed =-1 * yspeed;
cloud4.gotoAndPlay(2);
//removeMovieClip(cloud4);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud5.hitArea)){
yspeed =-1 * yspeed;
cloud5.gotoAndPlay(2);
//removeMovieClip(cloud5);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud6.hitArea)){
yspeed =-1 * yspeed;
cloud6.gotoAndPlay(2);
//removeMovieClip(cloud6);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud7.hitArea)){
yspeed =-1 * yspeed;
cloud7.gotoAndPlay(2);
//removeMovieClip(cloud7);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud8.hitArea)){
yspeed =-1 * yspeed;
cloud8.gotoAndPlay(2);
//removeMovieClip(cloud8);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud9.hitArea)){
yspeed =-1 * yspeed;
cloud9.gotoAndPlay(2);
//removeMovieClip(cloud9);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud10.hitArea)){
yspeed =-1 * yspeed;
cloud10.gotoAndPlay(2);
//removeMovieClip(cloud10);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud11.hitArea)){
yspeed =-1 * yspeed;
cloud11.gotoAndPlay(2);
//removeMovieClip(cloud11);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud12.hitArea)){
yspeed =-1 * yspeed;
cloud12.gotoAndPlay(2);
//removeMovieClip(cloud12);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud13.hitArea)){
yspeed =-1 * yspeed;
cloud13.gotoAndPlay(2);
//removeMovieClip(cloud13);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud14.hitArea)){
yspeed =-1 * yspeed;
cloud14.gotoAndPlay(2);
//removeMovieClip(cloud14);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud15.hitArea)){
yspeed =-1 * yspeed;
cloud15.gotoAndPlay(2);
//removeMovieClip(cloud15);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud16.hitArea)){
yspeed =-1 * yspeed;
cloud16.gotoAndPlay(2);
//removeMovieClip(cloud16);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud17.hitArea)){
yspeed =-1 * yspeed;
cloud17.gotoAndPlay(2);
//removeMovieClip(cloud17);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud18.hitArea)){
yspeed =-1 * yspeed;
cloud18.gotoAndPlay(2);
//removeMovieClip(cloud18);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud19.hitArea)){
yspeed =-1 * yspeed;
cloud19.gotoAndPlay(2);
//removeMovieClip(cloud19);
//xspeed =-1 * xspeed;
}
if (this.hitTest(cloud20.hitArea)){
yspeed =-1 * yspeed;
cloud20.gotoAndPlay(2);
//removeMovieClip(cloud19);
//xspeed =-1 * xspeed;
}
};