Deleting object type



  • Could I have an example or a link about deleting the same object type within five tiles of that object?

    Space Castle is a free retro style platformer game with a mystery storyline and made using the haxeflixel library.

    0


  • @galoyo
    Are the objects to remove tiles?



  • sprites because i would like to tween them before removing them but tiles are ok too. what ever is easiest.

    Space Castle is a free retro style platformer game with a mystery storyline and made using the haxeflixel library.

    0


  • This post is deleted!


  • This post is deleted!


  • @galoyo

    If you add all your objects to a FlxGroup, and each is its own class, like MyObject1.hx, MyObject2.hx, MyObject3.hx, etc., you can do something like this.

    NOTE: The function argument objType Class<FlxObject needs a closing > but it gets flagged as spam if I include it. So add it.

    	private function removeObjects(tileMidPoint:FlxPoint, tileSize:Float, objects:FlxGroup, objType:Class<FlxObject, tilesAway:Int=5):Void
    	{
    		for (i in 0...objects.members.length)
    		{
    			var obj:Dynamic = objects.members[i];
    			
    			if (Std.is(obj, objType))
    			{
    				var xDist:Float = Math.abs(tileMidPoint.x - obj.getMidpoint().x);
    				var yDist:Float = Math.abs(tileMidPoint.y - obj.getMidpoint().y);
    				
    				var tilesX:Int = Math.ceil(xDist / tileSize);
    				var tilesY:Int = Math.ceil(yDist / tileSize);
    				
    				if (tilesX <= tilesAway && tilesY <= tilesAway)	// within tilesAway tiles
    				{
    					obj.kill();
    					objects.remove(obj);					// you can remove it from the group if you want
    				}
    			}
    		}
    	}
    
    

    To use it

    // you need a group of objects
    var myObjects:FlxGroup;
    myObjects.add(new MyObject1(0, 0);
    myObjects.add(new MyObject1(100, 200);
    myObjects.add(new MyObject2(100, 100);
    myObjects.add(new MyObject2(100, 150);
    myObjects.add(new MyObject3(200, 100);
    // and so on...
    
    // make this the object you're checking the distance from
    var fromObj = new FlxObject(); 
    
    // get the midPoint of the from object
    var fromPoint:FlxPoint = fromObj.getMidpoint();
    
    
    removeObjects(fromPoint, myObjects, MyObject1, 5);
    
    var tileSize = 16;	// tile size of your tiles, change to what you have
    var tilesAway = 5;	// distance in tiles to remove objects
    
    // remove all type MyObject1 from group myObjects within tilesAway tiles of fromPoint
    removeObjects(fromPoint, tileSize, myObjects, MyObject1, tilesAway);
    
    

    example object MyObject1.hx

    package;
    
    import flixel.FlxG;
    import flixel.FlxSprite;
    
    class MyObject1 extends FlxSprite 
    {
    	
    	public function new(x:Float=0, y:Float=0) 
    	{
    		super(x, y);
    		
    		loadGraphic("assets/images/myobject1.png", true);
    		animation.add("animate", [0, 1, 2, 3]);		// if it's an animated spritesheet
    		animation.play("animate");
    	}
    	
    	override public function update(elapsed:Float):Void 
    	{
    		// update code ...
    		
    		super.update(elapsed);
    	}
    	
    	override public function kill():Void 
    	{
    		// kill code ...
    		
    		super.kill();
    	}
    	
    	// other override functions if you need them ...
    }
    


  • @galoyo

    Hey, I had a look at this again and noticed a problem so I edited my post. I wasn't accounting for the tile size. I guess I should ask you what your game does so I'd know how to approach this. Is it like a grid that you click a tile and the surrounding tiles, that are the same type, within 5 disappear? Something like that? If so, this could be done differently.



  • @dean
    This game I would like to make is similar to a metroid game, a platformer, that can have 4 exits per map, up, down, left or right. I was thinking that it would be easier to make a door out of 5 blocked that would represent an exit instead of 4 different sprites..

    After I posted the first message in this thread, i thought that tween every instant of the sprite because the other doors cannot be seen when at a door, so maybe it would be easier to tween every instant then delete them instead of what I first posted but this I do not know how to do.

    Space Castle is a free retro style platformer game with a mystery storyline and made using the haxeflixel library.

    0


  • @galoyo

    Well, that code is not right for that then.

    So, you have four doors, and you want to animate it when the player contacts it, and switch map or something? So it looks like he walked through the door and enters a new map?

    I would make a class, Door.hx that is just an animated sprite. Add four of them to a FlxGroup and check overlap between the player and the door group in your PlayState update(). Then if the player touches the door you can execute the code you need and animate the door, etc.

    If that sounds about right I can give you some example.



  • @galoyo

    I fixed a mistake with this, sorry about that. You don't need to define DoorID. Just use the builtin ID.

    Door.hx

    package;
    
    import flixel.FlxSprite;
    import flixel.group.FlxGroup;
    
    class Door extends FlxSprite 
    {
    	
    	// public static function doorID:Int; // DON'T NEED THIS
    	
    	public function new(x:Float=0, y:Float=0, id:Int) 
    	{
    		super(x, y);
    		
    		// my door is a 4 frame sprite sheet. make it how you want
    		loadGraphic("assets/images/door" + id + ".png", true);
    		animation.add("open", [0, 1, 2, 3, 3, 3, 2, 1, 0], 16, false);
    		
    		// this id gets used to know which door you're at
    		// doorID = id; // CHANGE TO
    		ID = id;
    	}
    	
    }
    

    PlayState.hx

    	private var _doors:FlxGroup;
    	private var _player:Player;	// if you have a Player class, or make it a sprite
    
    	override public function create():Void
    	{
    		
    		// I hope you have a Player class, or make this your player object
    		_player = new Player(0, 0);
    		
    		_doors = new FlxGroup();
    		
    		// change the x, y values as you need
    		// the third arg is the ID of the door
    		_doors.add(new Door(0, 0, 0));	// west
    		_doors.add(new Door(0, 0, 1));	// north
    		_doors.add(new Door(0, 0, 2));	// east
    		_doors.add(new Door(0, 0, 3);	// south
    		
    		add(_doors);
    		
    		super.create();
    	}
    	
    	override public function update(elapsed:Float):Void 
    	{
    		super.update(elapsed);
    		
    		// add the ovelap check to PlayState update() that calls 
    		// the function doorFunction() when player overlaps the door
    		FlxG.overlap(_player,  _doors,  doorFunction);
    	}
    
    	private function doorFunction(player:FlxObject, door:FlxObject):Void 
    	{
    		// animate the door
    		door.animation.play("open", false);
    		
    		// with the ID set for each door you can...
    		Switch (door.ID)
    		{
    			case 0:		// west
    				// code to execute for left door
    				
    			case 1:		// north
    				// ...
    				
    			case 2:		// east
    				// ...
    				
    			case 3:		// south
    				// ...
    				
    		}
    		
    		// animate the player with a tween to look like he goes throught the door.
    		// move player to center of door and fade him out
    		var px = door.x + door.width / 2 - player.width / 2;
    		var py = door.y + door.height - player.height;
    		
    		FlxTween.tween(player, { x: px, y: py }, 0.25 ).then(FlxTween.tween(player, { alpha: 0 }, 0.25));
    	}
    


  • @dean
    the player walks to the right door or top door, there will be five blocks for those doors and the blocks will animate then the player will go to the next map.

    sounds correct.

    Space Castle is a free retro style platformer game with a mystery storyline and made using the haxeflixel library.

    0

Log in to reply