ok, here´s a first time noob question, sorry if that´s stupid.
I was just wondering, for a battleship kind of game, would it be a waste of memory to build a set of objects for each cell (10X10=100), with position(x,y) and state(empty,hit,missed) properties?
I was wondering if it would be better to only create an object Grid and use methods to calculate the cell positions whenever necessary (when handling cell selection with touches or drawing for example)..
The former is problematic because you may have ships that sit side-by-side or end to end and it will become difficult to know when one is completely destroyed just from the data structures you described. Two hits side-by-side could be two hits on the same ship, two hits to two different ships, or even a sinking for the smallest ship.
Go with the latter for sanity's sake.
If I was doing this, I would keep it simple, Have a 2 dimensional array, that's your 10 by 10 grid.
When someone takes a turn, calculate the position and;
if it's a miss, insert a 0 in that array cell
if it's a hit, insert a 1 in that array cell
Related
Normally tables are easy to manage , especially since autolayout can do most of the heavy lifting to determine cell sizes and so on. However, there are cases where things become a little bit more tricky like having a table of polls.
A poll in this case consists of labels with variable description lengths, images with varying sizes and a set of options for the user to choose from ( a nested table essentially ).
----Approach 1------
Break each poll into a number of cells
Pros:
Its easy to add more fields to a poll in the future, layout is easier to handle for a single cell for each element especially in the case of the nested table. If you need to fall back to manually calculating the size of a specific cell you can do it while keeping everything else on automatic.
Cons:
If you have multiple polls you need to keep track which cell corresponds to which poll, and you generally need to do some arithmetic to keep track of whats what.
----Approach 2-----
Have the poll that contains all these elements be one big cell and set the constraints between them appropriately.
Pros:
Its easy to implement, maps your model to your cell in a one to one fashion without having to multi-demultiplex your data.
Cons:
Autolayout can get hairy with nested tables. For example, for the height of the cell in this case, i used to return UITableViewAutomaticDimension until i realised this wont cut it as a nested table exists that could be any kind of size. Its quite straightforward to calculate the contentsize of the nested table, however, this would mean you would have to calculate ALL of the other elements as well as now you're returning a specific height and not automatic. Your job has become that much harder.
----Outro----
How do you approach issues like that? Approach 1, 2 or something entirely different? I've simplified the structure of the elements here to make things a bit more readable, there's more arcane stuff involved like shadowed views containing the elements you see to create the illusion of a floating card etc.
Thanks everyone.
P.S : If there's already an answer about this somewhere please point me towards that, i just haven't found much on this yet. Hopefully this post can spark some discussion on the matter and help other people in my shoes.
A straightforward alternative suggested by Paulw11 is to use sections and Approach 1. Even though this may not cover cases that have tables within tables within tables, it will suffice for what I imagine is mostly every conceivable case that has a respectable UI.
I have more SKSpriteNodes, randomly generated (then 3, 4 or more) that all belong to the category CNPhysicsCategoryA and I have another single sprite that belong to the category CNPhysicsCategoryB. I need to check when all sprites in category CNPhysicsCategoryA collide all toghether simultaneously to the one in CNPhysicsCategoryB. There's a way to do this? Maybe with allContactedBodies, but how use it, can anybody help me with the code? Thank you so much...
Make a variable called numberOfCollisions. Inside the update function make it equal to 0. The update function is the first thing that gets called in the rendering cycle so at the start of every frame numberOfCollisions is equal to zero.
Then every time the didBeginContact function is called increase it by one and check if it is equal to the number of objects in CNPhysicsCategoryA.
I have a symbol of a person standing, he have child movieClips like hands, legs, head, body....so then if i press a button or click in the stage how do i need to change the animation instantly?
I suppose:
1) create a new movieClip that has all the same parts but animated as walking(for example) and then make something like:
if(keypressed)
{
person.replaceSymbol(myNewAnimatedSymbol); //method that not exist
}
2) for example if my standing animation longs from 1 to 30, i can make another animation in the same line let's say from 31 to 60 where he is running and then write:
if(keypressed)
{
person.legs.gotoAndPlay(31); //in the 60 frame he go back to 31 by using gotoAndPlay(31)
person.hands.gotoAndPlay(31);
person.body.gotoAndPlay(31); //and so on...
}
Can you give me an advice of which of methods I need to use? or if you have a better idea how to make it...
Hm-m-m. I'd say your second approach is easier to both draw and control, especially if it'll eventually come to reskinning your MC, say your person will get dressed in a cloak that's attached to the body and arms, your "new animated symbol" will likely require a change to accommodate that clothing, while in case of one symbol you can assign its parts another look, like say person.hands.cover=clothing.handpart; person.legs.cover=clothing.legpart etc, and then the animation will contain references to cover being changed, so that correct parts of clothing will appear around all the moving parts.
In fact, you might not need separating your person into different body, hands, legs etc MCs, but instead make it so that your person's legs are a container that can hold either a bare body part (naked leg drawing) or a dressed body part (a boot, pants, etc), and animate the main part by making these containers move according to how a person should walk, stand, run, attack etc etc., so that you will have a single point to dress the entire set of your person's animations.
It depends for example my character has around 100 animations. Putting them in a single timeline and telling them to go from this label to that label (or frame) is a huge mess to manage.
So I have 100 library items with export symbols. I keep on switching between them. This makes it easy to manage.
So, I think the question is how many animations do you have? Can you manage them in a single timelines? If you can , I'd say go with labels or frame jumps.
Both of your methods are correct and your second method would be faster because you're not adding or removing anything from the display list.
I have a little logical problem over here.
As the title says, I try to build a boardgame as a computer-program (maybe with internet-support, but thats another story)
As for now, I have a map, which has some crossroads in it, hence I cannot simply define the fields as '1, 2, 3, 4, ...' because if there is a crossroad at field 10, I would have more than one field which has to be labeled 11 (Because then there is a field left and right of field 10, for example.)
So the problem is, if I cannot define the Board in numbers then I cannot simply get the possible positions a player can take when he rolls 2d6-dices with calculating 'Field-Nr. + RandomRange(1,6) + RandomRange(1,6)'
Does anybody have an idea, how to define a Map like this on another way, where I still can calculate the possible new-fields for Player X with a 2d6-dice-roll?
Thanks in advance.
If i understand well... (i don't thing so) this might help you. Just use dynamic arrays for your boardgame field and change your actions after the dimensions x,y .... Look at this "type Name = array of {array of ...} Base type; // Dynamic array"
It sounds like you have a graph of connected vertices. When a player is at a particular vertex of N edges, assuming N < 12, the new Field will be reached from traversing edge number N % ( rand(6) + rand(6) ).
You could also just do rand(12), but that would have an even distribution, unlike 2d6.
Instead of dynamic arrays, I would recommend using a linked-list of records to describe the surrounding cells, and traverse the player's location and possible moves using that linked-list.
First, define a record that describes each cell in your board's playable grid (the cells on the grid can be four-sided like a chessboard, or hexagonal like in Civilization V) ... each cell record should contain info such as coordinates, which players are also in that cell, any rewards/hazards/etc that would affect gameplay, etc. (you get the idea).
Finally, the linked-list joins all of these cells, effectively pointing to any connected cells. That way, all you'd need is the cell location of Player X and calculate possible moves over n amount of cells (determined by the dice roll), traversing the adjoining cells (that don't have hazards, for example).
If all you want is to track the possible roads, you can also use this approach to identify possible paths (instead of cells) Player X can travel on.
I am a beginner at ActionScript3, and for my learning purpose, I am trying to build a match3 kind of game. I am making is a clone of bejeweled kind of game. but instead of swapping, I have to delete those elements and shift the upper elements down and add new elements above those shifted elements.
I am able to delete matched elements and after matching I'm removing those elements but I am stuck with the shifting code. I am not able to shift those elements down.
I believe that you should take a look at Richard Lord's Tetris source code as your game may be somehow similar in mechanics to Tetris. Tetris shifts down rows when you get lucky.
Richard Lord is one of the Flash Gurus and his way of doing things may seem pretty advanced for a starter like you. Take a look over the source code and see if it fits you. What I can tell is that this is the proper way to make a game but maybe it's not the best point to start for a novice like you that is in urgent need.
http://www.richardlord.net/blog/actionscript-3-tetris-source-code
I would approach it like this in 2 parts.
Loop through your array of board positions starting from the bottom row and check for an empty unoccupied slot, if you find one do another loop through the row above till you find a piece on the same column. If you find a piece on the row above apply a tween to move it down to the empty slot and continue your loop.
Once you have looped through all rows, and animated all the tweens you need to loop through again to find all the gaps that need gems dropped into them. Create new elements at those positions and then move them up by (element height * row), apply another tween to animate them back to their starting position.