creating two different sprites from the same class



  • Could I have two or more doors, each a different type, one small, one large, from within the same door class? I can load the image... loadGraphic("assets/images/door" + i + ".png" and even the animation with "i" but for the collide ... FlxG.overlap(door, player, doorFunction); I cannot pass the i variable. Is this all possible?



  • Assuming the first parameter of doorFunction is an Int, you can bind the i variable like this:

    FlxG.overlap(door, player, doorFunction.bind(i));
    


  • @galoyo

    The door and player objects get passed in the doorFunction(), so you can use the door ID and do a switch on the door.ID. Add the id:Int param to the Door new() and in the new() block set the ID = id. Then when you add each door, you give it an id. _doors.add(new Door(100, 100, 1)); When you load the image say ... "assets/images/door" + id + ".png" ...

    In the doorFunction

    private function doorFunction(door:FlxObject, player:FlxObject):Void
    {
    	switch (door.ID)
    	{
    		case 1: // door 1
    			
    		case 2: // door 2
    			
    		case 3: // door 3
    			
    		case 4: // door 4
    		
    	}
    }
    


  • @galoyo

    and the overlap should specify the _doors FlxGroup that you added all the doors to.

    FlxG.overlap(_doors, player, doorFunction);

    I made an edit to the original code I posted for this. I had defined doorID for some stupid reason, when you can just use the builtin ID. Sorry about that.



  • @dean
    I have tried this... switch (door.ID){case the first time you posted it at the link you just posted. the switch case code at PlayState seems to be ignored. please verify.



  • @DleanJeans
    I have changed the code to... FlxG.overlap(door, player, doorFunction.bind(_door.ID)); and the function has Int instead of Void. I now get an error message, Missing return Int but there is an return statement at the bottom of the function. The next error is Int should be flixel.FlxSprite



  • @galoyo

    You won't need to bind. What I think you're missing is to add all the doors to a FlxGroup.

    private var _doors:FlxGroup;
    

    and in create()

    // change the coords to where you want. third arg is the id.
    _doors = new FlxGroup();
    _doors.add(new Door(100, 100, 1));
    _doors.add(new Door(100, 100, 2));
    _doors.add(new Door(100, 100, 3));
    _doors.add(new Door(100, 100, 4));
    

    and in update() it's the group of all doors

    FlxG.overlap(_player,  _doors,   doorFunction);
    
    

    and doorFunction()

    private var doorFunction(player:FlxObject, door:FlxObject):Void
    {
    	switch(door.ID)
    	{
    		case 1: // door 1
    		case 2: // door 2
    		case 3: // door 3
    		case 4: // door 4
    	}
    }
    

    and Door.hx - add the id param and set the ID

    package;
    
    import flixel.FlxG;
    import flixel.FlxSprite;
    
    class Door extends FlxSprite 
    {
    	// add the id param
    	public function new(x:Float=0, y:Float=0, id:Int) 
    	{
    		super(x, y);
    		
    		// set the ID here.
    		ID = id;
    		loadGraphic("assets/images/door" + id + ".png", true);
    		animation.add("open", [0, 1, 2, 3], 16, false);
    	}	
    }
    
    


  • to get the rest of that code at the link to work, i needed to change private function doorFunction(player:FlxObject, door:FlxObject):Void to private function doorfunction(door:FlxSprite, p:Player):Void



  • changing the code to ID = id; made the switch statement work. :)



  • @galoyo

    ok, good. I didn't test the code examples, so I could have made a mistake. I didn't test this one either, so... but at least it shows the principle. :smile:


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.