Thursday 15 September 2011

javafx - How to avoid a 'machine gun' effect to the bullets in my game? -



javafx - How to avoid a 'machine gun' effect to the bullets in my game? -

some days ago asked this: how have multiple instances on screen of same sprite @ same time javafx2 , partially solved question elaborating suggestion of jewelsea.

i have obstacle now: when key pressed 'fire' bullets, weapon shoot bullets fast machine gun.. limit amount of bullets weapon of hero of game can shoot..for illustration decide shoot bullet every 0.5 secs or when key pressed , not have machine gun effect... in game part of programme controls 'fire' effect this:

scene.setonkeytyped(new eventhandler<keyevent>() { @override public void handle(keyevent event2) { if (event2.getcode()==keycode.f); { .........

before i've tried using setonkeypressed , setonkeyreleased same results.. seek shoot bullet keeping press 'f' key or limit bullets in number? give thanks in advance , bye!

i've done using timeline timer , starting , stopping on key pressed , key released:

import javafx.animation.animation; import javafx.animation.keyframe; import javafx.animation.timeline; import javafx.application.application; import javafx.scene.scene; import javafx.scene.input.keycode; import javafx.scene.layout.pane; import javafx.stage.stage; import javafx.util.duration; public class keyeventtest extends application { @override public void start(stage primarystage) { pane root = new pane(); scene scene = new scene(root, 400, 400); duration firinginterval = duration.millis(500); timeline firing = new timeline( new keyframe(duration.zero, event -> fire()), new keyframe(firinginterval)); firing.setcyclecount(animation.indefinite); scene.setonkeypressed(event -> { if (event.getcode() == keycode.f && firing.getstatus() != animation.status.running) { firing.playfromstart(); } }); scene.setonkeyreleased(event -> { if (event.getcode() == keycode.f) { firing.stop(); } }); primarystage.setscene(scene); primarystage.show(); } private void fire() { // dummy implementation: system.out.println("fire!"); } public static void main(string[] args) { launch(args); } }

it's easy adapt additionally limit number of bullets on screen @ time, etc.

javafx sprite keyevent eventhandler

No comments:

Post a Comment