Author Topic: Bullet code bug?  (Read 2219 times)

0 Members and 1 Guest are viewing this topic.

Offline turiqwalrus

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 840
  • Rating: +51/-2
  • Wheeeeeee~!
    • View Profile
Bullet code bug?
« on: May 21, 2012, 01:31:14 pm »
So, hellninjas and I have been trying to figure out some decent code for programming bullets, and we've pretty much got it down now- except for one bug.
when firing a new bullet, a picture of the bullet sprite bullet flashes briefly at the top of the screen.
Could someone find what's wrong?
Also, optimize if neccesary :P
Spoiler For code:

0->C+5 -> D
15 -> X -> Y
[18247EBDFFBDFFA5] -> Pic1
[183C3C3C3C240000] -> Pic2
Repeat getkey(15)
X+getKey(3)-getKey(2) --> X
Y+getKey(1)-getKey(4) --> Y
Pt-On(X,Y,Pic1
if getkey(54)
DS<(D,5)
X->{C+1*2+L1}
Y->{C+1*2+1+L1}     
C++
end
end
if C
for(L,0,C)
{L*2+1+L1}-1->{L*2+1+L1}
if {L*2+1+L1}>64
if C>0
Copy(C*2+L1,L*2+L1,2
end
C--
else
Pt-On({L*2+L1},{L*2+1+L1},Pic2
end
end
end
DispgraphClrdraw
end

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Bullet code bug?
« Reply #1 on: May 21, 2012, 05:05:18 pm »
Your issue arises from this block of code:

Code: [Select]
If C
For(L,0,C)
.Snip
End
End

I see two issues with this. First, because of how you add bullets, the first bullet is located at L1+2, whereas this loop structure treats L1 as the location of the first bullet. Secondly, this loop will run 0 times if C equals 0, but it will run C+1 times if C doesn't equal 0. For instance, if C equals 1, the loop will iterate for L equals 0 and 1, for two iterations total. This extra iteration in combination with reading data at L1 that's not a bullet is what's causing the issues you're seeing.

Thankfully, the fix for this is really easy. It even allows you to completely remove the If C block surrounding the for loop! Just change the lower bound of the for loop to 1. :)

Code: [Select]
For(L,1,C)
.Snip
End

Alternatively, the most optimized way to fix this is with your own loop structure. :P

Code: [Select]
C
While
→L
.Snip
L-1
End



And just for fun, here's my optimized version of the entire program. It's also attached. ;D

Spoiler For Spoiler:
Code: [Select]
.BULLET
0→C→D
15→X→Y
[18247EBDFFBDFFA5]→Pic1
[183C3C3C3C240000]→Pic2
While 1
 Pt-On(getKey(3)-getKey(2)+X→X,getKey(1)-getKey(4)+Y→Y,Pic1)
 !If D
  If getKey(54)
   X→{Y→{C++*2-2+L1}+1}
   4
  End
  +1
 End
 -1→D
 C
 While
  -1→L
  {*2+L1→P}--
  Pt-On({}ʳʳ,/256,Pic2)
  !If -L6
   Copy(C--*2+L,P,2)
  End
  L
 End
 DispGraphClrDraw
EndIf getKey(15)

If the code box is tiny, that's because Chrome fails. Click "Select All" and then press Ctrl+C to copy it, then just paste it into something like notepad to view it.
« Last Edit: May 23, 2012, 12:13:18 pm by Runer112 »