for dysfunction of the loop

I made this small AS3 practice thing, where you can shoot [with a spaceship] on 'enemies' and get extra points.

I wanted to make a function that enemy respawns after hitting them.

I have the spawning enemies using a loop for.

I also remove the enemies when they are hit.

the problem now is: for some strange reason, the game keeps spawning enemies and ignoring the loop for...


all variables are defined at the beginning of the script, so I won't include this part:

These are loops that make enemies spawn

for (var teller1 = 0; teller1 < 8; teller1 ++) {}
addvijand1 (null);
}
for (var teller2 = 0; teller2 < 8; teller2 ++) {}
addvijand2 (null);
}


adding the addlisteners

stage.addEventListener (Event.ENTER_FRAME, drainage); irrelevant for now: it's controls
stage.addEventListener (Event.ENTER_FRAME, schieten); It's the addchild for balls, with included hittest which removes the movieclip of the ball and the enemy and should lessen bij teller1 and teller2 one.
stage.addEventListener (Event.ENTER_FRAME, addvijand2); addition of the enemies of type 1
stage.addEventListener (Event.ENTER_FRAME, addvijand1); addition of the enemies of type 2 //this is basically the same [just forms and colors] type 1 in the code, so I'm not going to include to make it easier to forget.


function addvijand2 {(evt:Event)}
var vijand2: MovieClip = new MovieClip();
vijanden2.push (vijand2);
vijand2. Graphics.beginFill (0x22FF00);
vijand2. Graphics.drawCircle (-5, 10, 10);
vijand2.x = (Math.random () * 260) + 20;
vijand2.y = (Math.random () * 200) + 10;
var glow2:GlowFilter = new GlowFilter();
glow2. Color = 0x22FF00;
glow2.Alpha = 1;
glow2.blurX = 20;
glow2.blury = 20;
glow2. Quality = BitmapFilterQuality.HIGH;
vijand2.filters = [glow2];
addChild (vijand2);
}

function schieten (evt:Event) {}
If {(spaceKeyDown)
var mc: MovieClip = new MovieClip();
mc.graphics.beginFill (0xFFCC00);
mc.graphics.drawCircle (3,3,3);
var glow: GlowFilter = new GlowFilter();
Glow.Color = 0xFFCC00;
Glow.Alpha = 1;
glow.blurX = 10;
glow.blurY = 10;
Glow.Quality = BitmapFilterQuality.HIGH;
MC.filters = [glow];
MC.x = mc_schip.x;
MC.y = mc_schip.y;
mc.addEventListener (Event.ENTER_FRAME, moving);
addChild (mc);
}
}


function moving (evt:Event) {}
evt. Target.y-= 20;
for (var teller1:int = 0; teller1 < vijanden1.length; teller1 ++) {//here it searches if the ball hits an enemy of the table, deletes it if necessary.

If ((vijanden1[teller1]!=null) & & (evt.target.hitTestObject (vijanden1 [teller1]))) {}
MovieClip(vijanden1[teller1]).removeEventListener (Event.ENTER_FRAME, vijand1animatie);
removeChild (vijanden1 [teller1]);
Score += 2;
scoretext. Text = "Note:" + score;
vijanden1 [teller1] = null;
teller1 +=-1;
}
}
for (var teller2:int = 0; teller2 < vijanden2.length; teller2 ++) {}
If ((vijanden2[teller2]!=null) & & (evt.target.hitTestObject (vijanden2 [teller2]))) {}


removeChild (MovieClip (vijanden2 [teller2]));
score += 10;
scoretext. Text = "Note:" + score;
vijanden2 [teller2] = null;
teller2 +=-1;
}
}
If (evt. Target.y < 0) {}
MovieClip (evt.target) .removeEventListener (Event.ENTER_FRAME, moving);
removeChild (MovieClip (evt.target));
}
}

I think that the error is with the stage.addEventListener (Event.ENTER_FRAME, addvijand...);  part, because it is an ENTER_FRAME event, so he adds enemies of each image.

But even when I move the loops in the functions of addvijand1/2, or change them, while loops, it still does not work...

What I am doing wrong?

Since I have nothing better to do here is a more dynamic version with more features. Now enemies explode, and the ship can be moved with the arrow keys left and right. In addition, it eliminates the need for both enemy who makes hit faster detection:

stop();
import flash.display.Graphics;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.filters.GlowFilter;
import flash.geom.ColorTransform;
import flash.geom.Transform;
// reusable instances
var enemy:MovieClip;
var bullet:MovieClip;
// abstract Gprahics
var g:Graphics;
// ship speed
var shipSx:Number = 3;
// enemy types data
var enemyTypes:Object = {      enemy1: {color: 0x00CCFF, shape: "circle", radius: 10, speed: 2, score: 2},
                                   enemy2: {color: 0x22FF00, shape: "rectangle", side: 20, speed: 1, score: 10}
                              }
var enemies:Array = [];
// loop iterator
var i:int = 0;
var score:Number = 0;
//these are the for loops that make the enemies spawn
makeEnemies();

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);

function makeEnemies():void {
     for(i = 0; i < 8; i++) {
          enemies.push(makeEnemy("enemy1"));
          enemies.push(makeEnemy("enemy2"));
     }
}

function onKeyDownHandler(e:KeyboardEvent):void {
     switch(e.keyCode) {
          case 32:
               addEventListener (Event.ENTER_FRAME, schieten);
          break;

          case 39:
          case 37:
               // set ship move direction
               mc_schip.dx = e.keyCode == 39 ? 1 : -1;
               mc_schip.addEventListener (Event.ENTER_FRAME, shipMove);
          break;
     }

}

function shipMove(e:Event):void
{
     mc_schip.x += shipSx * mc_schip.dx;
}

function onKeyUpHandler(e:KeyboardEvent):void {
     switch(e.keyCode) {
          case 32:
               removeEventListener (Event.ENTER_FRAME, schieten);
          break;

          case 39:
          case 37:
               mc_schip.removeEventListener (Event.ENTER_FRAME, shipMove);
          break;
     }
}

function makeEnemy(type:String):MovieClip {
     var data:Object = enemyTypes[type];
     enemy = new MovieClip();
     g = enemy.graphics;
     g.beginFill(data.color);
     switch(data.shape) {
          case "circle":
               g.drawCircle(0, 0, data.radius);
          break;

          case "rectangle":
               g.drawRect( -data.side * .5, -data.side * .5, data.side, data.side);
          break;
     }
     g.endFill();
     // add data to the object for further use
     enemy.data = data;
     placeEnemy(enemy);
     enemy.isAlive = true;
     enemy.filters = [glowFilter(data.color)];
     enemy.addEventListener(Event.ENTER_FRAME, enemyMove);
     return enemy;
}

function enemyMove(e:Event):void
{
     enemy = MovieClip(e.target);
     enemy.y += enemy.data.speed;
     if(enemy.y > (stage.stageHeight + 10)) placeEnemy(enemy);
}

function placeEnemy(enemy:MovieClip):void {
     enemy.x = Math.random() * stage.stageWidth;
     enemy.y = Math.random() * -100;
     addChild(enemy);
}

function schieten(e:Event):void {
     bullet = new MovieClip();
     bullet.graphics.beginFill(0xFFCC00);
     bullet.graphics.drawCircle(0, 3, 3);
     bullet.filters = [glowFilter(0xFFCC00, 10)];
     bullet.x = mc_schip.x;
     bullet.y = mc_schip.y;
     bullet.addEventListener(Event.ENTER_FRAME, moveBullet);
     addChild(bullet);
}

function moveBullet(e:Event):void {
     bullet = MovieClip(e.target);
     bullet.y -= 20;
     bewegen(bullet);
     if (bullet.y < 0) {
          bullet.removeEventListener(Event.ENTER_FRAME, moveBullet);
          if (contains(bullet)) removeChild(bullet);
     }
}

function bewegen(bullet:MovieClip):void {
     // for each loop is faster
     for each(enemy in enemies) {
          if (bullet.hitTestObject(enemy)) {
               // allows any bullet to die in exlosion
               // although only first hit adjusts score
               if (enemy.isAlive) {
                    enemy.isAlive = false;
                    enemy.addEventListener(Event.ENTER_FRAME, explode);
                    var trans:ColorTransform = new ColorTransform();
                    trans.color = 0xFFB895;
                    enemy.transform.colorTransform = trans;
                    score += enemy.data.score;
               }
               // bullet is used only once
               removeChild(bullet);
               bullet.removeEventListener(Event.ENTER_FRAME, moveBullet);
               bullet = null;
               // exit loop
               break;
          }
     }
     scoretext.text = "Score: " + score;
}

function explode(e:Event):void {
     var eo:MovieClip = MovieClip(e.target);
     eo.alpha -= .3;
     eo.scaleY += .5;
     eo.scaleX += .5;
     if (eo.alpha <= 0) {
          // retuen to original state
          eo.isAlive = true;
          eo.transform.colorTransform =  new ColorTransform();
          eo.removeEventListener(Event.ENTER_FRAME, explode);
          placeEnemy(eo);
          eo.alpha = eo.scaleY = eo.scaleX = 1;
     }
}

function glowFilter(color:uint = 0x00CCFF, blur:Number = 20):GlowFilter {
     var f:GlowFilter = new GlowFilter();
     f.color = color;
     f.alpha = 1;
     f.blurX = blur;
     f.blurY = blur;
     f.quality = BitmapFilterQuality.HIGH;
     return f;
}

Tags: Adobe Animate

Similar Questions

  • Incorrect display using the loop format for

    Hello

    I have a digital painting using a hexadecimal display format that is autoindexed by a loop for. Within the loop for each unique value is passed to a Subvi needed the hexadecimal display format. Unfortunately, indexing is not keep this hex format but transfers back to a decimal number.

    No idea how to solve this problem?

    Thanks in advance and best regards

    Simon

    Thanks for this tip. The problem was an another Subvi where I created the num-array that will subsequently in the loop for., all the digital elements where the value instead of I32 I16. The controller could not handle these false values, so I got a nonsense.

    Thank you very much for your quick responses!

  • A generated within a loop in a subvi output data can be transferred to the main program for each iteration of the loop?

    Hi LV users,.

    I have a very basic question, I have not succeeded to asnwer using basic considerations.

    I made a sub - vi that performs a scan of current-voltage using a unit of measurement-source Keithley and a loop FOR.

    The subvi outputs 2 tables with my data (essentially an array of voltage and the corresponding current table). I also defined a Terminal at the exit of a group of these 2 tables in order to plot a graph XY - output. Specifically, I indexed this cluster to update after each iteration of the loop FOR, in my sub - vi (the indicator is placed outside the loop, of course).

    My problem is that I want my main program to display the XY-graph in real time, with an update after each iteration of the loop FOR which is in my sub - vi.

    I have a problem because my sub - vi output terminals are available for the main program concluded as soon as the sub - vi has completed its own execution (which is what we expect of sub - vi to do).

    How can I use a sub - vi (because it's handy) and get in the main program in the course of its performance data that are generated from the loops of sub - vi?

    Thanks in advance for your help,

    Yoyo87

    Elements of the queue in the Subvi them put your main VI where you want to display the data and the.

    It is similar to the architecture of producer/consumer. There are examples of it in LabVIEW. The examples work with 2 parallel loops, in your case a loop (the producer) will be in the Subvi.

  • Cannot read every value of the loop iteration for

    Hiiii...

    I developed a front-end which reads the values of serial port and display on the front panel.

    16 channels, which is a string data value. I've separated this string to read the data of each channel, but at the end of the loop for, I can read data only one channel for an iteration, and also I have to store each value iteration in a text file.

    I'm new to labview. Help, please. I have LabVIEW 8.0.

    Attached is my application code.

    This seems quite inefficient. Why don't you use a business structure? Why don't post it your code.

    In regards to the original code, you try to run by using the run continuously? I ask because I do not see a global loop, so it was not clear whether this was intended to present itself as a simple Subvi or "on-demand". If it's supposed to run continuously until you stop, DO NOT USE CONTINUOUSLY RUN it BUTTON. Use a while loop in your VI. Do not forget to place the initialization and close outside of the loop - you don't need to initialize and close the serial port whenever you loop around.

  • Programming of a conditional FOR the timing of the STOP button on the loops

    I have a conditional FOR loop with a STOP button, however, the nature of LabVIEW data flow, the stop button is now being questioned at some point before the end of the loop. A control not having no entry, I can't connect to the last operation of a single iteration of the loop to trigger the vote on the STOP command, and I prefer not to use a flat sequence just for the power button if I can avoid it. I've considered using a structure of the event, but I don't want to force the loop to wait that the stop button to be pressed, I want just the loop to EXIT, * IF * the stop button is pressed... but I want the stop button to be questioned at the end of the loop, so if the user presses the button stop at any time in the loop , at the end of the iteration, the loop will end... that makes sense? At present, given that the stop button is called once at some arbitrary during the iteration of the loop time, if the user presses the stop button after he is questioned, the loop must run an extra iteration, and I don't want that to happen.

    ... in any case, I guess I'm looking for a more elegant way to implement that having to use a flat sequence just for the stop button.

    Sorry, but you'll have to use a sequence structure.  This is why they are there.  Sometimes they have their purposes.  Here is how I would handle it.

  • Automatic indexation 2D array in the loop For - what is happening?

    I found many sources dealing 1 d tables in a loop For or a While loop, using automatic indexing, but nothing on the tables of higher order.

    I work with a program that feeds a 2D array in a loop (see table).  From what I see, it looks like this the results of automatic indexing in a 1 d, the first column of table 2-D table.

    This is the expected behavior, and it would hold true for arrays of higher order, table 3D for example?

    Is it possible to refer to the second column rather than the first?

    wildcatherder wrote:

    I found many sources dealing 1 d tables in a loop For or a While loop, using automatic indexing, but nothing on the tables of higher order.

    I work with a program that feeds a 2D array in a loop (see table).  From what I see, it looks like this the results of automatic indexing in a 1 d, the first column of table 2-D table.

    N ° it auto-index through a line at a time table.  You will get a 1 d table which consists of all of the columns that make up each row in each iteration.

    This is the expected behavior, and it would hold true for arrays of higher order, table 3D for example?

    Yes.  Automatic indexing on a 3D Board will give you a table on each iteration of each page 2D.

    Is it possible to refer to the second column rather than the first?  This question applies once you understand the first response.

  • How to get the items on a loop at the same time during the execution of the loop for

    Hello

    I am a student. I would like to know how to get the outside loop counter values For in parallel so that the loop runs rather than obtaining the value finally outside the loop for future prospects for the answers.

    Thank you

    Frederick

    You already said yes, and you have said some of the different ways (registrants, locals, reference, queue, etc.). Since the information was provided to your request, the thread can be considered closed? If you want details about how to implement something, you must provide the details on what you are doing.

  • Why this disable structure encapsulating the loop for that I'm falling?

    In the middle of coding and debugging I noticed something weird... now I'm just curious.

    Here is what happened (audio WARNING may be a bit much, fans of pumping in a warehouse im in...):

    https://www.YouTube.com/watch?v=vC9BKJ0CwmY

    I'm really just curious to know why this is happening - it seems that there is the note of "make sure wire you the error" fault behind the loop that the disable struc overlapped a bit...

    Why the struc disable that first, I dropped in this video did seize all of the loop?

    -pat

    You have this decoration of the label on your block diagram behind your loop For. When you put the new business structure, he captured this label which is outside the loop For. This forces the case structure to take the loop too, since it is the only way to recover the property node and label.

  • Is there a way to reset displays step status for iterations of the successive loop (NOR with active follow-up sequence editor)

    I'm running a sequence in the sequence editor (single-pass) with active follow-up.  I find that when I get a section in my sequence loop, the displayed State of stage starts in white, is preparing for the first passage through each step of the loop, and the steps are displayed to this status, unless / until modified in a loop later.  I would prefer that the status of step back empty at the beginning of each iteration of the loop and then get / displayed when the step is completed (yet) in order to better show the progress through the wrist strap.  Is there a way to do this?

    Thank you Ray!

    I've attached an example of using the method suggested by Ray reference.

  • Value of the loop variable for begins with the last entry of the previous run

    I have a problem with a loop for... I am a beginner with Labview and perhaps do not follow the order of events (since it runs not on a line-by-line as typical of programming languages), but I think that my problem is trivial.

    Say I'm running a loop for 16 iterations and I want to build a table which, for example, consists of four 0s, 1 4s, four 2s and 3 four s.   I initialize the array outside and using shift registers to be added to the matrix in the loop for.

    However, the first time I run the program I get funky results.  In subsequent runs, the first value in my table is a 3, and the rest of the rest of the table is moved down by 1, so I did that three 3s at the end (for example).

    Can someone help me understand how to solve this problem?

    Thank you

    George


  • Is it possible to change the loop i for

    Hello

    A question of Labview. A defence should be repeated 10 times in a loop for. If the value of i in the for loop will go from 0 to 9. Sometimes, certain defences may repeat once extra. It there a way to change the value of i in the loop for?

    Thank you very much

    Parallel port wrote:

    Hello

    A question of Labview. A defence should be repeated 10 times in a loop for. If the value of i in the for loop will go from 0 to 9. Sometimes, certain defences may repeat once extra. It there a way to change the value of i in the loop for?

    Thank you very much

    literally, no.

    You can use a while loop where you control the number of iterations.

    It is possible to connect a value for "N" to limit the number of iterations, but if you are indexing a table via a tunnel of automatic indexation of the input array entry will also limit the number of iterations.

    Ben

  • For results outside the while loop

    Dear friends,

    I have a while loop vi, which is attached to this mail. I want to update Boolean Boolean results, while the while loop runs. In fact this is not the case?

    Do I need to use it as a global variable or another simple method to do? Could you please guide me, or please alter the VI of the sample which is attached to this mail.

    Thank you

    Best regards

    Tom

    The Boolean value outside of the loop only will be updated, as wiring system, when the loop ends, it's how the LabVIEW data flow. The simplest method for updating the Boolean value outside of the loop is to unplug the wire, create a local variable (right click on the device, select 'create', 'Local Variable'), move the room inside the loop and hook it up to your function "equal." This is not necessarily the best method, just the easiest for this example.

  • Table of path error 1430 in the loop for

    Hello!

    I have a problem with the 1430 error: path is empty or relative, which seems impossible. The thing is that I have a battery of indicators of path connected to the loop for (check the .jpg and there is no problem with writing to a file by using the paths of an array with the first and last index.) Others are not saved because of the error... to be honest, that all data paths are absolute. Record in the file of the idicator path is the work of a Subvi (merge data vi) but it is in a for loop because I need to make it available to multiple files. There is no error that occur with the work of Subvi to a path. The question is: what labview makes with the paths saved in a table between the loops? I have just run out of ideas so if someone had some time, I would really appreciate for all the tips and ideas.

    K.

    Hello again,

    If someone was interested in the happy ending: the solution is: 2 loops for. I think there must be something with the way that labview sends data between iterations. In any case - don't know how much exactly but works

    the floor is .jpg.

  • The sum of several waveforms, created inside the loop for

    Hello

    I am currently viewing the Gibbs phenomenon by adding several sine waves. The issue I'm having is that I can't understand how to add sine waves created in the loop. I have attached a picture of the basic structure for my project below. I tried to use simply "add" with a feedback loop, but it seems that my programming skills apply here. Any suggestion is appreciated. Thank you!

    0

    Something like that?

    1. make sure that you "reset" the generation of signals with each iteration

    2. with the waveforms, please make sure that the sample rate is the same.  This may be a problem with the first iteration.  Then add in a check to use only the first waveform on the first iteration.

  • Value of the loop 'For' pass before the complete loop - FPGA

    Hello

    8.5 LV, LV FPGA, PCI 7831-R FPGA Board

    I got a cramp of brain on this one.  I have a function (Arb. GIS read) that Im using to generate an arbitrary signal which I created in memory.  I can't move to the value of data however.  I don't know why I can not, its because it is nested in a "loop For" who runs indefinetly and updates only the value whenever it loops back to 'zero '.  Ideas quick and dirty on how to use this value as it is being updated in the loop in my hand vi?

    I've seen messages on the use of local variable 'Files' and property nodes, but I can not just give a sense the.  Maybe because it's FPGA, something is different/no supported?

    * My principal is 'control MicroMirror Arb. SP", look in the #4 case and the condition of"false. "

    The 'Sub - VI' is called 'Arb Sig read RevB', and I'm trying to pass the variable 'Data' to hand while the loop For always runs.

    Thank you!


Maybe you are looking for