Add Sprite to RectMapLayer

45 views
Skip to first unread message

Bastien Sevajol

unread,
Jun 27, 2017, 1:32:54 PM6/27/17
to cocos2d discuss
Hello,

I'm trying to use cocos.tiles.RectMapLayer in this context:

  • A cocos.scene.Scene construct with:
    • A cocos.layer.scrolling.ScrollableLayer containing:
      1. A background image: cocos.sprite.Sprite
      2. A "ground" layer: cocos.tiles.RectMapLayer (built from cocos.tiles.load and tmx file)
      3. A "subjects" layer: cocos.tiles.RectMapLayer (manually built)
      4. A "top" layer: cocos.tiles.RectMapLayer (built from cocos.tiles.load and tmx file)

The "subjects" layer is built manually:


    def get_subjects_layer(self) -> cocos.tiles.RectMapLayer:
       
# TODO: hardcoded values: use TMX infos
        cells
= self.generate_subjects_cells(width=70, height=40, tile_width=8, tile_height=8)
       
return cocos.tiles.RectMapLayer(
            id
='subject',
            tw
=8,  # hardcoded values: use TMX infos
            th
=8,  # hardcoded values: use TMX infos
            cells
=cells,
       
)

   
def generate_subjects_cells(
       
self,
        width
: int,
        height
: int,
        tile_width
: int,
        tile_height
: int,
   
) -> typing.List[typing.List[cocos.tiles.RectCell]]:
        cells
= [[None] * height for x in range(width)]  # type: typing.List[typing.List[cocos.tiles.RectCell]]
       
for n in range(width * height):
            i
= n % width
            j
= height - (n // width + 1)
           
# TODO: Give here the tile
            cells
[i][j] = cocos.tiles.RectCell(i, j, tile_width, tile_height, properties={}, tile=None)
       
return cells

My goal is to add some sprites to "subjects" layer. But, i can see what cocos.tiles.RectMapLayer.cells, so cocos.tiles.Cell require a cocos.tiles.Tile. How can i use my sprites with a cocos.tiles.RectMapLayer ?


Regards,

Bux.

claudio canepa

unread,
Jun 27, 2017, 2:39:51 PM6/27/17
to cocos2d discuss
Hi, Bastien

I don't see what is exactly your intent, it can be one of

1. add static decoration (sprites that don't move in world coords) that maybe don't align to a grid using a tmx layer as source info

2. add actors that can roam the world, using starting position and other info loaded from a tmx layer info to build the sprites

3. add 'special' tiles that can't be loaded directly from a tmx layer (why can't be loaded ?)

Maybe others. Can you clarify ?



 


--
You received this message because you are subscribed to the Google Groups "cocos2d discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cocos-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to cocos-...@googlegroups.com.
Visit this group at https://groups.google.com/group/cocos-discuss.
For more options, visit https://groups.google.com/d/optout.

Bastien Sevajol

unread,
Jun 27, 2017, 3:42:41 PM6/27/17
to cocos-...@googlegroups.com

Hi,

My intent is the number two: add actors who will move on a grid specified in the tmx file. I thought RectMapLayer can help me about that with this "grid" management and functions like get_at_pixel/get_in_region (to select actor with click for example).

Final goal is to make an interface like Close Combat series game:

cc3

Where:
  • background image: cocos.sprite.Sprite: is the background image (grass, roads, ...)
  • "ground" layer: cocos.tiles.RectMapLayer: are eventual tiles added in tmx map
  • "subjects" layer: cocos.tiles.RectMapLayer: are soldiers
  •  "top" layer: cocos.tiles.RectMapLayer: Trees
But maybe RectMapLayer is not the right choice for "subjects" layer ?

Regards,
Bux.
You received this message because you are subscribed to a topic in the Google Groups "cocos2d discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cocos-discuss/WOdnOu_7y44/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cocos-discus...@googlegroups.com.

claudio canepa

unread,
Jun 27, 2017, 4:27:18 PM6/27/17
to cocos2d discuss
On Tue, Jun 27, 2017 at 4:42 PM, Bastien Sevajol <sevajol...@gmail.com> wrote:

Hi,

My intent is the number two: add actors who will move on a grid specified in the tmx file.

That will not work with RectMapLayer; it can work with  tiles.TmxObjectLayer plus mapcoliders.TMXObjectMapCollider
 

I thought RectMapLayer can help me about that with this "grid" management and functions like get_at_pixel/get_in_region (to select actor with click for example).

If the actors quantity is big, the selection can be too slow; then it would be better to use collision module (theres an example of mouse picking with collision module in samples\ mouse_elastic_box_selection.py , available from the cocos repo ar the zip downloaded from pipy)

 
To unsubscribe from this group and all its topics, send an email to cocos-discuss+unsubscribe@googlegroups.com.

To post to this group, send email to cocos-...@googlegroups.com.
Visit this group at https://groups.google.com/group/cocos-discuss.
For more options, visit https://groups.google.com/d/optout.

Bastien Sevajol

unread,
Jun 28, 2017, 2:44:22 AM6/28/17
to cocos-...@googlegroups.com

Hi,

Thank's for these informations. I will take a look about all of that and go back here or in new thread if questions appears.

Thank's again,
Bux.

To unsubscribe from this group and all its topics, send an email to cocos-discus...@googlegroups.com.

Bastien Sevajol

unread,
Jun 28, 2017, 4:45:11 PM6/28/17
to cocos-...@googlegroups.com

Hello,

I'm tring to reproduce (copy-paste mouse_elastic_box_selection.py) the MinMaxRect and ProbeQuad. ProbeQuad is working, but MinMaxRect is not visible. When i debug/look at logs, python correctly enter in begin_drag, adjust_elastic_box, etc functions. But rectangle selection is not visible.

I hope it will be not to much difficult to enter in my code, but to reproduce:

Setup commands:

git clone https://github.com/buxx/synergine2.git test_bux_cocos
cd test_bux_cocos/
git checkout tile
virtualenv --no-site-packages -p /usr/bin/python3.5 venv3.5
source venv3.5/bin/activate
pip install -r requirements.txt

Run the script:
python sandbox/tiledstrategy/run.py maps/003
You should see a top view map, with some trees and some actors moving. ProbeQuad is visible under mouse. When click, drag and move, terminal who run command print the following log:
begin drag
begin drag selection: drag_selecting, drag_moving True False
But no rectangle appear.

Interesting source code is layer construction here and layers classes (copy-paste mouse_elastic_box_selection.py copied code is here) here.

Regards,
Bux.

claudio canepa

unread,
Jun 28, 2017, 10:58:49 PM6/28/17
to cocos2d discuss
Is missing the method draw :)
Copy it from the cocos samples and it will work. Tested.

Nice trick you pulled with your 'Setup commands", the novelty make me follow along against good sense, otherwise I would have replied with the "please post a small script reproducing your problem" 

For any other reader: this will not work again.

For future you: I probably will not put more than 15' in investigating any further problem in an extended codebase, so try to ask over small sample code.

I invested 2+ hours in this, to illustrate:

"""
I managed to adapt the instalation instructions to venv and Windows, packages installation seems to terminate normal doing
    python -m pip install --upgrade pip
    python -m pip install --use-wheel psutil
    # del psutil line from requirement.txt
    python -m pip install -r requirements.txt


But when I try to run the script

(venv3.5) F:\dev\test_bux_cocos>python sandbox/tiledstrategy/run.py maps/003
Traceback (most recent call last):
  File "sandbox/tiledstrategy/run.py", line 11, in <module>
    from sandbox.tiledstrategy.simulation.base import TiledStrategySimulation, T
iledStrategySubjects
  File "F:\dev\test_bux_cocos\sandbox\tiledstrategy\simulation\base.py", line 2,
 in <module>
    from synergine2.xyz import XYZSimulation
  File "F:\dev\test_bux_cocos\synergine2\xyz.py", line 7, in <module>
    from synergine2.simulation import SubjectMechanism, Subjects, Subject
  File "F:\dev\test_bux_cocos\synergine2\simulation.py", line 247, in <module>
    class SubjectBehaviourSelector(BaseObject):
  File "F:\dev\test_bux_cocos\synergine2\simulation.py", line 250, in SubjectBeh
aviourSelector
    behaviours: typing.Dict[typing.Type[SubjectBehaviour], object],
AttributeError: module 'typing' has no attribute 'Type'

Now, I have a ubuntu 17.04 machine for testing some other code, but psutil will not install there (compilation fails because no python headers. I know they can be had by apt-get  install python-dev, but that is a global command and I don't want to risk the other work I m doing.)

Trying to run the script at this point failed.

Searching for 'psutils' in ~/dev/test_bux_cocos/*.py the only use seems to be in tests/test_processing.py , so I deleted the psutil line in requirements.txt and rerun the pip install.

Then tried again the script; this time it runs.

Ok, it not draws the selection box.

Where to look? Multiple posibilities, of the top of my head
  - how is the scene composed ? it is at the right layer ?
  - coords conversion problems ?
  - is the code from the cocos samples copied verbatim or there is some subtle change ?
 
Looking a bit over your pointers to code; scene composition... too much to read, editor... hang me if I know if it is verbatim, random scrolling... , mhhh . This going nowhere, I have no time...; lets see if anything on the selection box draw code has changed ... bingo!

Wrote reply
"""

I'm not angry, just explaining why probably seeking help over an extended codebase will not work.

Cheers!

 


Bastien Sevajol

unread,
Jun 29, 2017, 3:05:24 AM6/29/17
to cocos-...@googlegroups.com

Hello,

Thank for your investigation. Actually it was not a strategy to present a setup commands list to "hide" code complexity. Sorry for that, be sure i will make only small script in future.

In addition of that my error was terribly simple (missing the method draw) even if I spent hours to debug ...

Thank you again.
Bux.

You received this message because you are subscribed to a topic in the Google Groups "cocos2d discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cocos-discuss/WOdnOu_7y44/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cocos-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages