Creating attack pattern 3

To finish off all the attack moves for the boss, let's create a third attack pattern script.

Prepare for lift off

For this attack pattern, we are going to use an interesting mathematical formula called a sine wave. You might recognize this pattern from mathematics or from a radio wave readout. This pattern causes the sprite to move up and down in a regular curved wave pattern.

Prepare for lift off

It looks very good and can be quite hard to dodge if you're not careful. It serves as a perfect "killer" move to close this part of the tutorial with.

Engage thrusters

This third attack pattern script starts and closes in exactly the same way as the other two. We start with receiving the message pattern3 and close by picking a new random pattern from the list, as follows:

  1. For this pattern, we also use set rotation style <left-right>, since it looks better when the sprite is facing straight ahead.
  2. We then use glide 1 secs to x: () y: () and set it to the middle-right corner of the screen, filling in the values 170 and 0 respectively. This will be the starting position.
  3. We use point in direction () to make the sprite face left; fill in value -90.
  4. Then we add a repeat () loop that will run 500 times.
  5. This pattern requires a slower horizontal speed, so we move speed / () steps each cycle of the loop.

The finished script will look like the following screenshot:

Engage thrusters

Now we get to the interesting part. We will use a sine wave formula to determine the vertical position of the sprite based on its horizontal position. This is why we needed to slow down the horizontal movement. At regular speed, the movement would be way too fast and bouncy.

  1. So after moving horizontally, we use set y to (). This slot will be filled with a formula that is constructed from different parts.
  2. First, we take the x position and place it in the left slot of the () * () block. In the right slot, we enter the value as 0.5. The resulting number determines how frequently the sprite completes a full wave (up-down and back to center). The higher the number, the shorter the wave.
  3. Then we take this result and multiply it by 4 (using multiply). This number determines the overall speed of the wave motion. It's an optional number. It's not necessary to complete the formula, but it gives us more control.
  4. Then we use the mathematical sin function on this result. This is what effectively causes the wavy pattern.
  5. Finally, we multiply the whole by 150 (using multiply). This part determines the amount by which the wave deviates from the center line. This number is literally the maximum vertical distance from the center line.
  6. After completing the formula, we just have to check whether the sprite is on edge and bounce if this is the case (using if on edge, bounce). The finished script will look like the following:
    Engage thrusters
  7. Double-check how the sine formula is assembled. If these blocks are mixed up and are running in the wrong order, we might get strange results. The calculations start from the inner blocks and progress towards the outer blocks.

Objective complete – mini debriefing

This concludes our last attack pattern. We used a sine wave formula to create a smooth, waving pattern for our boss sprite. Now we can fully test the boss enemy's movement. Let's test it a few times to see how it looks. If desired, you can experiment with the different values for the movement patterns. The sine wave can especially change a lot if you play around with the numbers for a bit. Just keep in mind the previous explanation to estimate what changing a certain number will do to the wave.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.59.219