0

My game is based on two screens (A & B) side by side, but the device screen can only display one of them at a time.
A is for example at position (0, 0) and B is at (320, 0)

I tried two solutions to switch from A to B:

  • First, I place the whole scene into one node, the MainNode. To switch from A to B, simply set MainNode position to (0, -320).
  • Other solution, more elegant IMHO (but not for LearnCoco2D who uses the Coco2D library), is to move the scene anchorPoint to (0, -1)

Now, if I want to go from A to B with an animation, these two solutions must be adapted:

  • By using a SKAction

    [Main runAction:[SKAction moveToY:-320 duration:0.1]];

  • By animating anchorPoint in the update method

    if(anchorY > -1) anchorY -= 0.1;

These two solutions works (despite a linear SKAction::timingMode does not render a linear translation properly), but I wonder which is the best in term of optimization, and elegance. Documentation is welcomed ;)

EDIT:

Apparently, my question is not clear (maybe due to my english level).

In few words, my question is: What exactly are the best practices for scrolling a scene?

Community
  • 1
  • 1
Martin
  • 11,881
  • 6
  • 64
  • 110
  • What exactly is the question here? – sangony Jan 31 '15 at 20:04
  • Hmm... the question could be "What exactly are the best practices for scrolling a map." – Martin Feb 01 '15 at 00:08
  • test them both on differently sized screens/phones to see how they act under changing scene sizes. I think the safest bet is to put everything you need to move in a subnode of the scene, then change subnode's position. Changing Anchorpoint can have unwanted side effects. – CodeSmile Feb 01 '15 at 11:20

1 Answers1

2

I am surprised (0, -1) doesn't throw a huge exception. According to the docs you get a range from 0 to 1. This could cause issues in the future. See the docs here

Changing your position sounds like a more elegant way of handling it. however changing it to negative -320 in a span of 1/10th of a second is rather quick and could explain why it looks funny. Also if you aren't making sure you are only calling that once it will look really odd. I would make sure that it is only getting called once and maybe using a bool to toggle if it should be moved instead of checking a position.

If you are going back and forth a lot from one screen to another this might be an ok solution. However if you are looking to scale this to a much larger map where you are going to transition several times to new screens I would recommend a different approach all together. Like creating a new node off screen when you need it and transition a parent node then pop the old node off.

I hope that helps.

Edit

Now that the question is "what exactly are the best practices for scrolling a scene".

I would recommend what LearnCoco2D mentioned in the comment and what I eluded to in my original answer.

  • Add a sub node to your scene that will handle positioning (lets call it mapNode)
  • Add any sprites that represent the scene to the mapNode
  • Move just the MapNode position on update

In the past I have built my Scenes in a similar fashion and have handled the scene position based on the player position in the update loop. I was able to keep the player in the center of the screen as he walked around the map. Might be getting off subject, but that is what I found the best practice for handling scrolling a scene from my experience. The project I am working on can be viewed here

I hope that answers your question.

Skyler Lauren
  • 3,792
  • 3
  • 18
  • 30
  • It's not explicitely mentionned that anchorPoint is in [0 1] range is Apple doc. – Martin Feb 01 '15 at 00:12
  • 1
    Aye the diagram does show you 0,0 to 1,1 but does not say you have to. Your question wasn't clear and I tried to answer it for you. What exactly is your question? – Skyler Lauren Feb 01 '15 at 01:19
  • Two people saying "what is the question", maybe the question isn't clear :) I edited my question. – Martin Feb 01 '15 at 09:57
  • Thanks for your update. You're describing the first method in my question, unless your are updating your map position in `update` method (because your map moves dynamicly). However, I'm not accepting your answer right now cause I would like to get other advices about. +1 anyway – Martin Feb 02 '15 at 08:13
  • I'm accepting the answer because my question do not ask for a specific one, but I would have wanted more feedbacks. – Martin Feb 11 '15 at 09:07