COMMUNITY

Removing json objects from array


(Lewis Lepton) #1

something im currently working on is using JSON to build levels, using tiled editor. now im only using it for the object group & to use the x, y, width, height of created object, then using those objects to then create entities within my game. im using kha to do the main brunt of things.

so after getting a good & working system for cycling thru a maps objects, they are rendering fine. but the deletion of them is something im having trouble. ive worked & do work using array.splice(object, 1); for removing. but working with json is different, or seems different to me

what happens is that when I hit just one coin, is that all of them are deleted & im just scratching my head on this more & more now.

here is the original code I had before I made things cleaner. the layer I am accessing is called ‘coin’, since im looking to collect coins

var layername = 'coin';
		arCoin = new Array<Coin>();
		for (l in 0 ... map.layers.length){
			for (o in 0 ... map.layers[l].objects.length){
				var objectlayer = map.layers[l].name;
				objectlayer = layername;
				// trace(objectlayer);
				if (map.layers[l].name == objectlayer){
					arCoin.push(new Coin(map.layers[l].objects[o].x + map.layers[l].objects[o].width / 2, map.layers[l].objects[o].y + map.layers[l].objects[o].height / 2, map.layers[l].objects[o].width, map.layers[l].objects[o].height));
				}
			}
		}

this did work, but the code is horrible looking & cack. so after a bit longer, I drummed up this, which has the sort of same effect, but is much better & does spit out the id’s of each object in the ‘coin’ layer

private static function setupCoin(){
		var layername = 'coin';
		arCoin = new Array<Coin>();
		for (layer in map.layers){
			for (object in layer.objects){
				if (object.name == layername){
					trace(object.id);
					arCoin.push(new Coin(object.x + object.width / 2, object.y + object.height / 2, object.width, object.height));
				}
			}
		}
		trace(arCoin.length);
	}

now this does spit out the actual id’s of the objects in the layer & even spits out the amount of the array, which on tests, is 3 objects. id’s are something like, 13, 50, 51.

this is the deletion code I was using way before when using XML. but after choosing to change to JSON, which I wanted to, it does not have the same effect. instead of deleting each object after it is collided with, it actually deletes all of them, then printing out that there is now 0 in the array

public static function destroyCoin(){
		var e = arCoin.length;
		while (e --> 0){
			arCoin.splice(e, 1);
		}
		trace(arCoin.length);
	}

this delete code would then go into a collision function that when the player gets into that, then it acts upon it. but as said, is deleting everything, rather than just one thing.

ive not fully used JSON as much as XML, & do know they are different & XML would be easier. but id personally like to use JSON in the end

does anyone have any pointers or methods that could help. I have been doing the JSON stuff for I think now 3 days & whilst im super happy I got each object to now create in each layer, its the deletion that im having so much trouble with. so I figured folks in the haxe community may know.

if there are examples, thats great, as I can look at it & learn from. but really, at this point, anything would be great to hand

though I am wondering is it that I have to access the id’s, but then think that wouldn’t be the case, since im looking for the created object number rather than the id. but really, ive tried so many different things. obviously my creation code is probably crap as well. but hey, im just really getting into JSON with map creation :wink:

thanks & all the best

lewis lepton


(Lewis Lepton) #2

also I know I have something called layername - but I have not changed that with the newest code :wink:

also I do know that you can remove objects from a JSON file. but since using it. its not let me remove. so maybe my header is not rightly titled :wink:


(Lewis Lepton) #3

here is my TypeDef as well if that helps

typedef LevelData = {
	layers:Array<{
		color:String,
		draworder:String,
		id:Int,
		name:String,
		objects:Array<{
			height:Int,
			id:Int,
			name:String,
			rotation:Int,
			type:String,
			visible:Bool,
			width:Int,
			x:Int,
			y:Int
		}>,
		opacity:Int,
		type:String,
		visible:Bool,
		x:Int,
		y:Int
	}>
}

(Simon Krajewski) #4

I’m having some trouble understanding what you’re asking exactly. Your destroyCoin function iterates the entire arCoin array and removes each element. There’s no condition in there that would restrict this to specific coin objects.

You probably want something like this instead:

public static function destroyCoin(coin:Coin) {
    arCoin.remove(coin);
}

And then you call that when your Coin collides with something.


(Lewis Lepton) #5

cheers @Simn - I did have a play a few days ago even with that remove(), but didnt work well, for ‘reasons’. though after some recoding & extra things, seems to have worked. which is making my mind melt a bit, since ive spent a long time working on this.

ive been used to using while loops & do know about that condition, but even trying with other tests & now looking at my original deletion code with the while, I did miss quite a bit. so will work a little bit more on that & see what else I can drum up.

ive seen my errors. now ill work on fixing them. cheers man :wink: x