Problems with collisions
-
What up all. My problem is with collisions. Basically I have a wall object that when you press Z a certain amount of times, it loses health and then once it reaches 0, it's position gets added by FlxG.width, essentially moving it exactly one screen over. however, when the play moves to the wall again, they're able to pass right through it. This does not happen when the first wall is there and Im sure I've set FlxG.worldBounds correctly.
here's a bunch of relavent source code:
Playstateprivate var _jill:Jill; private var _wall:Wall; override public function create():void { super.create();//I've tested this with super.create(); before everything like so, as well as after everything near the bottom of the function _jill = new Jill(90, 44); add(_jill); _wall = new Wall(_jill.x + _jill.width + 4, 0); add(_wall); FlxG.worldBounds.set(0, -10, 160 * 60, FlxG.height + 20); } override public function update(elapsed:Float):Void { FlxG.collide(_jill, _wall); if (FlxG.keys.justPressed.Z) { hitwall(); } super.update(elapsed); } public function hitwall():Void { _wall.health -= 0.1; if (_wall.health <= 0) { _newWall(); } } public function _newWall():Void { _wall.x += FlxG.width; _wall.health = 1 * (_bustedWalls/2); }
SOme other classes for ref
Jillvar _up:Bool = true; public function new(?X:Float=0, ?Y:Float=0) { super(X, Y); loadGraphic("assets/images/jill.png", true, 51, 100); animation.add("idle", [0]); animation.add("wall", [1]); animation.play("idle"); drag.x = 50; } override public function update(elapsed:Float):Void { if (FlxG.keys.justPressed.X) { velocity.x += 15; FlxG.sound.play("assets/sounds/walk.wav"); //little code to make it look like walking if (_up) y += 8; else y -= 8; _up = !_up; } //this is when she hits a wall, for now she can be in any position but thats not my problem atm if (FlxG.keys.justPressed.Z) { FlxG.camera.shake(0.01, 0.1); FlxG.sound.play("assets/sounds/jillBounce.mp3"); x += 15; } if (FlxG.keys.justReleased.Z) { x -= 15; } if (FlxG.keys.pressed.Z) { animation.play("wall"); } else { animation.play("idle"); } super.update(elapsed); }
Wall
public var _health:Float; public function new(?X:Float=0, ?Y:Float=0) { super(X, Y); makeGraphic(20, FlxG.height); immovable = true; _health = 1; }
and full source code can be found here: https://github.com/ninjamuffin99/Rebident-Ebil
-
add a
trace(_bustedWalls);
to make sure it's not 0. I don't see where you increment it. If _bustedWalls is 0 _wall.health will be 0.public function _newWall():Void { _wall.x += FlxG.width; _wall.health = 1 * (_bustedWalls / 2); trace(_bustedWalls); trace(_wall.health); }
-
I did have it in hitwall(); but didn't include it in the above source code, here it is however
public function hitwall():Void { _wall.health -= 0.1; _wall.alpha -= 0.1 / (1 * (_bustedWalls/2)); if (_wall.health <= 0) { _bustedWalls += 1; FlxG.log.add(_bustedWalls); _newWall(); } }
-
I just noticed the link to the full project. I see.
trace the
_wall.health
in_newWall()
Are you using
_wall._health
anywhere for anything? I only see it in Wall class.I don't know why it doesn't work though.
EDIT: unless the alpha is causing it. maybe try disabling the alpha to try it. Other than that, I don't know.
-
_health was being used as a health substitute earlier because I though the problem may have been related to the health variable. The problem still persists even after disabling the alpha stuff.
when i trace _wall.health in _newWall() (and _health) it returns a negative value when I trace it like so
public function _newWall():Void { FlxG.log.add("new wall"); FlxG.log.add(_wall.health + "no underscore"); FlxG.log.add(_wall._health + "underscore"); _wall.x += FlxG.width; //_wall.alpha = 1; _wall.health = 1 * (_bustedWalls/2); //FlxTween.tween(_jill, {x: _jill.x + FlxG.width}, 1); _wall.updateHitbox(); }
-
little update: I solved the collisions by putting FlxG.worldbounds.set() into the update function. Not sure if its supposed to go there but oh well
-
you can probably just replace
FlxG.worldBounds.set(0, -10, 160 * 60, FlxG.height + 20);
withFlxG.worldbounds.set()
and take it out of update().