View Full Version : more shooting problems
mario_hater
January 18th, 2007, 06:34 PM
in a book i have i found a way to shoot things with a limit of them on the screen. The problem is , i think, the way i am referencing them. When i make them visible all the time only one of them works, and the others just stay at the top.
maxMis = 10;
for (i=0; i<maxMis; i++) {
attachMovie("missile", "missile"+i, i);
missile = _root["missile"+i];
missile._visible = false
missile.fired = false;
}
onEnterFrame = function () {
for (i=0; i<maxMis; i++) {
missile = _root["missile"+i];
onMouseDown = function () {
if (!missile.fired) {
missile.visible = true;
missile.fired = true;
missile._x = _xmouse;
missile._y = _ymouse;
}
};
if (missile.fired) {
missile._y -= 10;
if (missile._y<0) {
missile._visible = false;
missile.fired = false;
}
}
}
};
does anyone know why? Or a different way to reference them
SacrificialLamb
January 18th, 2007, 10:39 PM
This code only allows 10 to max then starts taking the oldest one’s off
maxMis = 10;
bn = 0;
for (i=0; i<maxMis; i++) {
attachMovie("missile", "missile"+i, i);
missile = _root["missile"+i];
}
onEnterFrame = function () {
for (i=0; i<maxMis; i++) {
missile = _root["missile"+i];
missile._y -= 10;
if (missile._y<0) {
removeMovieClip(missile);
}
}
};
onMouseDown = function () {
(bn+2)>maxMis ? bn=0 : bn++;
trace(bn);
missile = attachMovie("missile", "missile"+bn, bn);
with (missile) {
_x = _xmouse;
_y = _ymouse;
}
};
mario_hater
January 19th, 2007, 01:12 PM
i don't understand what I'm doing wrong. I copied the code directly from the book and it works in the game. Does is matter that the code was written in functions. The bit that checks weather the mouse was pressed was in one function. The bit that attaches the clips and gives them variables was in one. And the rest was in another.
SacrificialLamb
January 19th, 2007, 01:41 PM
yes that could mater. but it could also be some little thing that's not in the code. i think you should start with simpler code, you should understand what you are coding and know why it works not just copy from a book and say "i now know this"
mario_hater
January 19th, 2007, 03:06 PM
i understand the code its just that i dont understand why it wont work for me when it works in the flash file on the book. I have uploaded the file to show you.
http://uploadhut.com/view.php/320514.fla
SacrificialLamb
January 19th, 2007, 03:31 PM
Do not start using Scenes! They are the devil! They make exporting slower, the file bigger and make the whole app more complicated.
Also like you have tried to fix I would not have half the code on the frame and the rest on the MC.
I think the problem was tat you should not put an onMouseDown in a for loop and probably not even a onEnterFrame that will call it every time. That would make a onMouseDown for every time it loops and I think the new onMouseDown would over write the earlier ones. Notice in the game they used an if statement in the onEnterFrame and the for loop was inside that. There is a Key.isDown for the mouse is you want to make it a if statement but making a onMouseDown would probably be better than checking every frame
mario_hater
January 20th, 2007, 04:43 PM
i dont understand what your saying, what would the code be?
mario_hater
January 20th, 2007, 04:56 PM
OK. I fixed the code but without the break; in, it does not work. Why is this? I thought a break; took it out of the statement it was in.
onEnterFrame = function () {
onMouseDown = function () {
for (var i = 0; i<10; i++) {
var missile = _root["missile"+i];
if (!missile.fired) {
missile.fired = true;
missile._visible = true;
missile._x = _xmouse;
missile._y = _ymouse;
break;
}
}
};
for (var i = 0; i<10; i++) {
var missile = _root["missile"+i];
if (missile.fired) {
missile._y -= 5;
if (missile._y<00) {
missile.fired = false;
missile._visible = false;
}
}
}
};
onLoad = function () {
for (var i = 0; i<10; i++) {
attachMovie("Ammo", "missile"+i, 7000+i);
var missile = _root["missile"+i];
missile._visible = false;
missile.fired = false;
}
};
is that not what the break does?
SacrificialLamb
January 20th, 2007, 05:11 PM
Also you missed a “break” and a “_” in one of the _visible you have there. But here is the code with it working form the example that you tried to copy.
//number for missiles
maxMis = 10;
//make limited missile
for (i=0; i<maxMis; i++) {
//attach missile
missile = attachMovie("missile", "missile"+i, i);
//make new missile invisible
missile._visible = false;
}
onEnterFrame = function () {
//mouse down
if (Key.isDown(1)) {
//loop looking for non fired missile
for (i=0; i<maxMis; i++) {
//get name
missile = _root["missile"+i];
//is missile fired (i used _visible coz they are bothe the same)
if (!missile._visible) {
//if un used use
missile._visible = true;
//move to mouse
missile._x = _xmouse;
missile._y = _ymouse;
//stop loop so you only fire one at a time
break;
}
}
}
//loop for moveing and missile removel
for (i=0; i<maxMis; i++) {
//get name
missile = _root["missile"+i];
//test if it should be movieng
if (missile._visible) {
//move
missile._y -= 10;
//test if it's out off sceen
if (missile._y<0) {
//stop moveing
missile._visible = false;
}
}
}
};
the "break " stops the loop
mario_hater
January 21st, 2007, 09:45 AM
thanks :-)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.