I am making game in cocos2D , in that i am using TMX map in that. Now in that , I put stars as score, player has to collect the stars , so that its score will increase.
My problem is i want to run repeat forever action on every sprite. I don't add that sprite to the tile map but i just make one extra layer and in that, i put tiles with star property.
Now, Where i add this tile map, at that time i check all tiles in the meta layer with star property and i want to put star image there.
My question is, As i don't know the number of tiles with the star property, so we can't make that much object array of CCSprite. And I know that we can't add one CCsprite object in scene twice.
How can i proceed to this problem?
Any kind of help will be appreciated..
Related
I'm developing a Dungeon Crawler style game and set a bunch of position objects as a "grid" for the player walk and stored them in a parent object, like a list. So I wanted to use that same list for the enemy moviment but after watching many A* Pathfinding tutorials for weeks I didn't managed to have any ideas of how to solve it since I'm not using a regular 2D grid. Here's a reference.
I took a look at your image, and you do in-fact have a grid. Just because you placed your objects does not mean you can calculate the "grid" for them from the list.
I would suggest you create a function that you call when your level starts.. and it iterates through the children of that object and generates a data structure that allows you to use A* to calculate pathing. You can use the transform.position of each object to determine it's relative placement/adjacency.
There is many ways you can do this. One suggestion would be creating an invisible collider with a trigger (can be on an empty GameObject) in different areas on your grid. Give the enemy a default velocity and attach a collider to the enemy and select the checkbox for trigger so when the enemy moves onto the trigger, you can call OnTriggerEnter (from a script attached to the enemy or the invisible collider GameObject) and make it change its movement. How you want to implement the AI is up to you.
edit as per comment
put invisible game objects positioned in your scene to how you would envision them being in an array from a birds eye view. In a script assign them to an array. Get the enemy to lerp between points in the array based on your ai algorithm. https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html
I am using cocos2d-x 2.1.4 and would like to use the same image file e.g. spritesheet.png for
the tiles on a tmx map built using Tiled Map Editor as well as;
in-game sprites, which I usually make the spritesheet using TexturePacker.
The reason for combining is to reduce the number of draw calls, since there is enough texture space for the sprites and tiles to all be shared on one image. If I used TexturePacker with Tiled, I think I would have to reassign tile positions every time TexturePacker rearranges the tile images, so that is not a good approach.
I know I can get a CCTexture2D* from a CCSpriteframe, which I wanted to try to insert into CCTextureCache, so that the CCTMXLayer can pick up the texture (which is actually from a spritesheet). However, there is no method to do so.
What is the common technique of dealing with a combined spritesheet for both tiles and sprites in cocos2d-x?
Change the sort algorithm in TP to name, then prefix your tile sprite images with _ or some other character to make sure they come first in the sprite sheet. You may want to create dummy tiles to reserve space for potential future tiles to avoid the risk of the tile sprites being rearranged when you add some more tiles in between.
Regardless of that, one additional draw call isn't going to make a difference.
I'm self learning C++ and playing around 2D tile mapping.
I have been reading through this scrolling post here, which is based on this tiling tutorial.
Using the above tutorial and some help from the Pearson, Computer Graphics with OpenGL book I have written a small program that draws a 40x40 tiled world and a Sprite (also a tile).
In terms of drawing/render order, the map(or world) itself is that back layer and the Sprite is the forward most (or top) layer. I'm assuming that's a good way of doing it as its easier for 2 tiles to interact than a tile and a custom sprite or rectangle. Is that correct?
I have implemented a Keyhandling() function that lets you move the map inside the viewport using the keyboards arrow keys. I have a variable called offsetx, offsety that when a key is pressed increases or decreases. Depending on whether I assign the variable to the map or sprite, I can more one or the other in any direction on the screen.
Neither seems to work very well, so I assigned the variables to both (map and sprite) but with positive values for the sprite, and negative for the map. So upon a key press, this allows my Sprite to move in one direction whilst the map moves in the opposite direction.
My problem is, the sprite soon moves enough to leave the window and not enough to bring the more of the map into the scene. (The window only shows about 1/8th of the tiles at any one time).
I've been thinking all day, and I think an efficient/effective way to solve this issue would be to fix the sprite to the centre of the screen and when a key is pressed the map moves around... I'm unsure how to implement this though.
Would that be a good way? Or is it expected to move the viewport or camera too?
You don't want to move everything relative to the Sprite whenever your character moves. Consider a more complicated world where you also have other things on the map, eg other sprites. It's simplest to keep the map fixed, and move each sprite relative to the map, (if it's a movable sprite). It just doesn't make much sense to move everything in the world whenever your character moves around in the world.
If you want to render your world with your character always at the center, that's perfectly fine. The best thing to do is move the camera. This also allows you to zoom your camera in/out, rotate the camera, etc. with very little hassle in keeping track of all the objects in the world.
Be careful with your usage of the word "viewport". It means a very specific thing in OpenGL. (ie, look at the function glViewport). If your book uses it differently, that's fine. I'm just pointing this out because it's not 100% clear to me what you mean by it.
Im using Level Helper and SpriteHelper to create my sprites, images, levels and more importantly animations and physics.
Note, by physics i mean the debugdraw you can see on the simulator that is used for collision detection.
I created a swimmer and added physics to this. Done the code so the physics follows the swimmer around the pool as they move. I have now come to animate the swimmer, make the legs kick etc. Now when i load my game and only the first sprite of the animation is the outline for the physics. So i can see the legs kicking on the swimmer but the debugdraw mesh of the physics doesn't animate as well. Now this is not really a problem until for example my swimmer loses their legs (weird game i know). I change the animation to now a swimmer with no legs but again the physics mesh still shows the legs. So any collisions with stuff still happens where the legs were but they shouldnt. This make sense?
Is there a way to update the physics on the new animation or do i need to remove my whole swimmer and draw a new one?
Any help at all would be great. Thanks
This makes sense, since your sprite uses the same box2d mesh in both states. If you want to have a different collision behavior after altering your Sprite, you should assign another (smaller) mesh body.
Note that even in cocos2d side, your sprite still has the same container box with the new animation.
In order to keep using the SpriteHelper functionality you might want to create 2 different sprite-body sets: one with the full body, then after the "accident" replace it with the legless sprite.
Now, gameplay-wise, my opinion is that there shouldn't be any collision for the legs anyway. since they are moving, players will not find it weird to have them not colliding. You could use a mesh body with no legs and use it for both sprites. Except if you want to have different collisions as a gameplay feature (Like giving the player the choice to cut his legs in order to fit in a smaller cave etc)
So with Tiled, I can set Tile Properties directly on a tile before placing it on a map like so:
This is how I have done collision checking, by setting the collision property to 'true' and then checking the tile properties when moving a sprite.
However, I would like to add a 'teleport' tile. When the player walks on a specific tile, it takes them to a separate location.
The problem I am running into, is that when you set a property on a tile, you only get to set it once, and not on the tile instance. Meaning every tile would have the same teleport location.
Am I overlooking something? Is there a better way to go about doing this in Cocos2d in general?
You can use the object layer for this. Add an "object" (that's just a rectangle or point in Tiled) to the teleporter tile and use the object's properties to connect two locations together.
When you load the map you could walk over all objects to find the connecting objects. Then you know the two tile locations of the teleporter end points which you could store in a teleportation array. Every time your player moves to a new tile, check the teleportation array to see if the player is on one of the teleportation fields, and if he is, move him to the other teleportation tile.
Of course you could also check intersection with the object (rectangle) but since there's a chance that you might accidentally create an object (rectangle) that spans multiple tiles it seems more reliable to check these objects before the game starts.
Well this probably is the best way but it's what I've done. You could create a meta layer and have separate tiles for each teleporting pad. So when you check if the player is on teleportingpad1 you set the players location to receiverPad1 (which could be another tile, object in tiled, or just a point you set when you check for collisions). And you would just make another one e.g. teleportingpad2, teleportingpad3, etc. for more pads.