
-----------------------------------
amz_best
Fri Sep 05, 2014 10:58 am

More than just 1 appear, and not all dispear after maxy
-----------------------------------
What is it you are trying to achieve?
im trying to shoot bullets (more than 1 on screen), and i want each seperate bullet to disapear when they reach maxy

What is the problem you are having?
a) only one bullet will show up
b) if i do figure out how to add moar bullets, i expect they will all dispear when only 1 reaches maxy, any way to prevent?


Describe what you have tried to solve this problem
boolean and int values to see if anything would change

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
unfortunately, i cant attach files so here is my code...



%sey.mo.raz  upper(bullets) then
            currentBulletIndex := lower (bullets)
        end if
        var bullet := bullets (currentBulletIndex)
        initBullet (bullet)
        bullet.pos.x := 234
    end if


Also, 
type Vector2D :
    record
        x : real
        y : real
    end record

type Bullet :
    record
        pos : Vector2D % Position
        vel : Vector2D % Velocity
        visible : boolean
    end record

proc initBullet (var b : Bullet)
    b.pos.x := 0
    % ...
end initBullet

proc updateBullet (var b : Bullet)
    b.pos.x += b.vel.x
    b.pos.y += b.vel.y
end updateBullet

proc drawBullet (b : Bullet)
    if b.visible then
        % ...
    end if
end drawParticle

var bullets : array 1 .. 100 of Bullet
var currentBulletIndex := lower (bullets) % 1

initialize ()
loop
    % Input
    if onKeyPress (SHOOT_BULLET_KEY) then
        currentBulletIndex := (currentBulletIndex + 1)
        if currentBulletIndex > upper(bullets) then
            currentBulletIndex := lower (bullets)
        end if
        var bullet := bullets (currentBulletIndex)
        initBullet (bullet)
        bullet.pos.x := 234 % Align bullet with the player's ship
    end if

    % Update
    for i : lower (bullets) .. upper (bullets)
        updateBullet (bullets (i))
    end for

    % Draw Frame
    cls
    Draw.FillBox (0, 0, maxx, maxy, black)
    for i : lower (bullets) .. upper (bullets)
        drawBullet (bullets (i))
    end for
    View.Update ()

    Time.DelaySinceLast (1000 div 60) % ~60 FPS
end loop


-----------------------------------
amz_best
Fri Sep 05, 2014 4:57 pm

Re: More than just 1 appear, and not all dispear after maxy
-----------------------------------
Wow, that seems really cool only, i dont undetstand it xD        ik wat an array is, but i dont understand any of the code u wrote xD. im still new to turing and i hope u could explain dis 2 me more thourouly

-----------------------------------
Zren
Fri Sep 05, 2014 5:18 pm

RE:More than just 1 appear, and not all dispear after maxy
-----------------------------------
Thought as much.

Here's a simplier example.

Press space to "Fire Bullets"


var SHOOT_BULLET_KEY := ' '

var chars : array char of boolean
var bulletY : array 1 .. 10 of int
var bulletVisible : array 1 .. 10 of boolean
var currentBulletIndex := lower (bulletY) % lower() grabs the lowest index = 1

proc initialize ()
    for i : lower (bulletY) .. upper (bulletY)
        bulletY (i) := 0
        bulletVisible (i) := false
    end for
end initialize

View.Set ("offscreenonly")
initialize ()
loop
    % Input
    Input.KeyDown (chars)
    if chars (SHOOT_BULLET_KEY) then
        currentBulletIndex := (currentBulletIndex + 1)
        if currentBulletIndex > upper (bulletY) then
            currentBulletIndex := lower (bulletY)

            % Reset to "ship"
        end if
    end if

    % Update

    % Draw Frame
    cls
    put "currentBulletIndex: ", currentBulletIndex
    for i : lower (bulletY) .. upper (bulletY)
        put "bullets(", i : 3, "): y=", bulletY (i), "    visible=", bulletVisible (i), "    current=", i = currentBulletIndex
    end for
    View.Update ()

    Time.DelaySinceLast (1000 div 10) % ~10 FPS
end loop


Note how we cycle a limited number of "bullets".

The key is to make the array larger than the maximum number of bullets you'd ever need on the screen.

1. Make the bulletY(i) value increment every frame.
2. Make the bulletY(i) value increment every frame only if bulletVisible(i) is true.
3. Set bulletVisible(currentBulletIndex) to true when pressing the shoot key.
4. Set bulletVisible(i) to false when bulletY(i) is "off the screen".

-----------------------------------
amz_best
Fri Sep 05, 2014 6:09 pm

RE:More than just 1 appear, and not all dispear after maxy
-----------------------------------
wow that is amazing. i can see wat u did and it makes so much moar sense! i got how it works and i typed it out already! except now i dunno how to add the photos when you press space or how 2 make them go up in their sepetrately xD

-----------------------------------
Zren
Fri Sep 05, 2014 6:16 pm

RE:More than just 1 appear, and not all dispear after maxy
-----------------------------------
them go up in their sepetrately

What's your code?

More steps:

5. Load Bullet Picture. Only use one picture for all bullets, as we can redraw it multiple times.
6. Add a bulletX array.
7. For each bullet the arrays, draw the bullet picture at ( bulletX(i), bulletY(i) ).
8. Only draw a bullet if bulletVisible(i) is true.

-----------------------------------
amz_best
Fri Sep 05, 2014 6:27 pm

Re: More than just 1 appear, and not all dispear after maxy
-----------------------------------
Everytime i run it, it says argument is wrong type, is it becuz im using real numbers?




%sey.mo.raz  upper (bulletY) then 
            currentBulletIndex := lower (bulletY) 
[/code]

to 

[code]
View.Set ("offscreenonly") 
for i : lower (bulletY) .. upper (bulletY) 
    bulletY (i) := 0 
    bulletVisible (i) := false 
end for 
loop 
    % Input 
    Input.KeyDown (chars) 
    if chars (SHOOT_BULLET_KEY) then 
        currentBulletIndex := (currentBulletIndex + 1) 
        if currentBulletIndex > upper (bulletY) then 
            currentBulletIndex := lower (bulletY) 
[/code]

Sorry for teasing you with so many advanced concepts when your still on your first week.

-----------------------------------
amz_best
Fri Sep 05, 2014 7:12 pm

RE:More than just 1 appear, and not all dispear after maxy
-----------------------------------
As soon as i saw initialize(), i went back into my code and saw that its not initialized before the loop, so i simply added initialize() and now its working fine, but bullets dont appear when i press space.

-----------------------------------
Zren
Fri Sep 05, 2014 7:20 pm

RE:More than just 1 appear, and not all dispear after maxy
-----------------------------------
Where exactly are you drawing the bullets? I wrote http://i.imgur.com/Tc1Nt9i.png

-----------------------------------
amz_best
Fri Sep 05, 2014 7:22 pm

Re: More than just 1 appear, and not all dispear after maxy
-----------------------------------

%sey.mo.raz 