Skip to content

Instantly share code, notes, and snippets.

@JOELwindows7
Created June 12, 2021 18:36
Show Gist options
  • Save JOELwindows7/4f4ddf6789ff8da9efefeac4465e4d93 to your computer and use it in GitHub Desktop.
Save JOELwindows7/4f4ddf6789ff8da9efefeac4465e4d93 to your computer and use it in GitHub Desktop.
The Mouse input in Haxe (Games From Scratch)
// https://gamefromscratch.com/haxeflixel-tutorial-mouse-input/
import flixel.FlxState;
import flixel.FlxSprite;
import flixel.FlxG;
class PlayState extends FlxState
{
var sprite:FlxSprite;
var scaleFactor = 0.1;
override public function create():Void
{ super.create();
sprite = new FlxSprite();
sprite.loadGraphic(AssetPaths.enemy__png);
sprite.x = FlxG.width/2 - sprite.width/2;
sprite.y = FlxG.height/2 - sprite.height/2;
add(sprite);
}
override public function update(elapsed:Float):Void
{
super.update(elapsed);
if(FlxG.mouse.overlaps(sprite)){
if(FlxG.mouse.pressed){
sprite.setPosition(FlxG.mouse.getPosition().x - sprite.width /2 ,
FlxG.mouse.getPosition().y - sprite.height /2);
}
}
if(FlxG.mouse.justReleasedRight){
sprite.x = FlxG.width/2 - sprite.width/2;
sprite.y = FlxG.height/2 - sprite.height/2;
sprite.scale.set(1,1);
}
if(FlxG.mouse.wheel != 0){
sprite.scale.add(FlxG.mouse.wheel * scaleFactor,FlxG.mouse.wheel * scaleFactor);
}
}
}
// reposted by JOELwindows7 to delete the nightmare code box from that original article, UGH! (lmao ala tankman week 7)
// uh, GNU GPL v3.. yeah I guess. but I haven't changed anything so.. sorry. idk. just use whatever you like, I just prettified it.
// pls credit Games From scratch (the URL on top too) and me for prettyfying it. thancc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment