For a shooter, I've created a class that simulates the tracers (TracerMGun) of invisible projectiles
(MGun).
In doing so, when firing each act-cycle the objects of MGun create a new object of class TracerMGun at the
respective X/Y-position.
In order to simulate the dissolving of the tracers, I've implemented three methods in TracerMGun-
class:
- fadeTransparency (reduces the transparency value of each object)
- SizingDownTracer (scales the size of each object)
- changeColor (is supposed to change the green component of the object).
The first two methods work perfectly fine, but the changeColor doesn't at all, though its code
is written according to the same principle fadeTransparency/fadeTransparency are.
Does anyone know why?
Here's the full code of the TracerMGun:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; public class TracerMGun extends MGun { private int transparency = 255 ; private int GreenComponent = 0 ; private int BlueComponent = 0 ; Color TracerColor = new Color ( 255 , GreenComponent, BlueComponent); private int TracerSize = 4 ; private int SizeDownTracer; public TracerMGun () { GreenfootImage TracerMGunImage = new GreenfootImage (TracerSize, TracerSize); TracerMGunImage.drawOval ( 0 , 0 , TracerSize, TracerSize); TracerMGunImage.setColor (TracerColor); TracerMGunImage.fillOval ( 0 , 0 , TracerSize, TracerSize); setImage (TracerMGunImage); getImage().setTransparency(transparency); } public void act() { fadeTransparency (); SizingDownTracer (); changeColor(); } public void fadeTransparency () { if (transparency == 0 || TracerSize <= 1 ) { getWorld().removeObject( this ); } else if (transparency > 0 ) { transparency = transparency - 5 ; getImage().setTransparency(transparency); setLocation (getX(), getY() - 1 ); } } public void SizingDownTracer() { SizeDownTracer++; if (SizeDownTracer % 10 == 0 ) { TracerSize --; getImage().scale(TracerSize, TracerSize); } } public void changeColor() { GreenComponent = GreenComponent + 8 ; getImage().setColor(TracerColor); } } |