I'm using the SneakyInput Joystick to move my sprite, and TMX maps for my game. I added auto-camera-movement to my app, so the hero sprite is always visible. When I go through my map, the joystick goes off the screen (it stays where it's original position is). I made a HUD so that the joystick is always at the same position, and doesn't move along with the map. The joystick shows up on screen fine, but when I drag it it doesn't move my hero.
I've made HUDLayer a CCLayer in GameLayer, which is where I'm making the moveHero method. The joystick works, but when I move it across the map, the auto-camera-movement works, but the joysticks stay in there same position, and are out of view pretty soon. I know the problem is that I do [self setCenter....blah blah blah]; instead of only setting Level1 as the center. How do I only move the camera for Level1, and not HUDLayer?
Here is a link I found on how to use this class. It seems fairly straight forward. I think you want to change the method - (void)moveHero to:
- (void)moveHero; {
[self applyJoystick:leftJoystick toNode:hero forTimeDelta:1.0/60.0f]];
}
as it seems you already kept a reference to the left Joystick when you initialized the HUD.
Ok, if you want to centre the HUD layer based on those 2 points (the joystick and the other item), you need only lay them out in a 2D grid, and figure out the centre point from there. I'll assume you are working with an iPhone screen in landscape mode. Then the dimensions are 480x320. The centre of this screen is (240, 160), and so, I would use this centre for my joystick and other HUD item. So I would probably set my joystick at (10, 10), and my other item (assume dimensions of this item to be (x, y)) at ((470 - x), 10), which would give the items both a 10 pixel boarder around them.
Hope that Helps!
Related
I'm very beginner in Unity so please forgive if, this questions isn't so hard to answer:)
So, I have a text on a Canvas in the editor, it is okay, it's showing well on Scene editor and In Game as well.
But, when I added two Sprites, which going to be the player and the enemy, the positions of these sprites are behave a bit weird.
The text position is: x: -293 y: 195, when I'm modifying the position of the text it works fine.
When I add the sprites to x:0 y:0 and x:1 y:1, in the scene editor they appear in the left bottom corner, but when I check in the game, they placed in the middle of the screen.
My question is why the coordinates and the positions are so different on Scene (grey) and on Game (blue) ?
Because initialized render mode of Canvas in Unity is "ScreenSpace - Overlay". So it is shown on too big area in scene. If you want to work only in view field of camera, in inspector just change render mode of Canvas to "ScreenSpace-Camera" and drag your MainCamera to RenderCamera in inspector. Even if you use ScreenSpace-Camera, coordinate system of RectTransform (UI Objects transform) is different than Transform (Normal game objects transform)
in this view, if you get closer to the left-down corner of your scene, you will see your main camera area and Sprites that are in correct positions.
I hope this helps.
I am working on a game which has working similar to game monkey kick off. I have one ball bouncing on a place at the left side of screen and depending upon the position of the ball I apply linearImpulse to the ball on user touch so that it appears that the ball is kicked.
But what happens is when i apply impulse the ball goes out of the screen bounds. I dont want the ball to go out of the horizontal screen bounds whereas it can go out of the screen vertically.I tried using CCFollow but it doesn't give the realistic feel to the game flow.
I tried THIS tutorial, but dint help much.I managed to scroll the background,Only this part remains.
Any Ideas on how the ball should not move out of the horizontal screen boundary..? but in case of vertical boundary on the other hand it can go out of the bounds.
If you're not using physics, it's a simple bounds check. Assuming the screen width is 320 points this will keep the ball inside the screen horizontally:
CGPoint ballPos = ball.position;
ball.position.x = fmaxf(0, fminf(320, ballPos.x));
ball.position = ballPos;
UPDATE: I noticed you mentioned linear impulse. So you're using a physics engine. In that case, create a horizontal wall on both sides. See the cocos2d Box2D or Chipmunk templates how to enclose the screen with a collision border, then use only the left and right side borders in your game.
I'm trying to create a top down car game where the camera follows both the player and the player's rotation. I can get CCFollow to work easily, but I have had no success with CCCamera. I assume that I need the camera in order to make rotation follow the player (i.e. have the player facing up at all times) but I have had no luck on google.
Can anyone either provide a code snippet or a tutorial on how to create a rotation-following top down camera?
Cheers!
My suggestion: don't use the CCCamera.
Your game design requires the car to move over a track. In programming terms this is often much easier accomplished by keeping the car static, and instead moving the background underneath.
Assume your car is at the center of the screen. It's supposed to move from left to right. Instead of moving the car or the camera, move the background layer - just in reverse: move the background layer from right to left to make it seem like the car is moving from left to right.
The same is true for rotation. If you want the car to turn left, rotate the background in clockwise direction.
This is a lot easier and can be accomplished simply by changing the position and direction properties of the background layer. Note that you do not need to do this for each object in the background layer, it's sufficient to add all objects to the background layer in the appropriate positions and then just change the background layer properties. The layer's children will follow accordingly.
I have a problem regarding CCFollow. I am using CCFollow along with CCParallaxNode. I have added layers to parallax node and then apply runaction: on parallax node.
[pn runaction:[CCFollow actionWithTarget:sprite worldBoundary:CGRectMake(0, 0, 5600, 320)]];
but using this the sprite always runs at middle of screen. Is there any way to keep it at left of screen and also layers follow this sprite?
Thanx.
If you want your player to remain on the left side of the screen, then lock the players X position to a specific region that makes sense. You should also be moving the background and other elements of your game, not your player (with exceptions, to the players position visually on the screen).
I have game application (cocos2d) in which, a ball is to be shooted from a cannon, this ball travels through a trajectory path and land on a building. Now the problem is that according to the requirement I have to increase the size of the background image due to which my building image is not visible on first view and with the ball traveling i have to drag the background image with out disturbing the trajectory path of the ball. Can some one help me out in this regards. To clarify question bit more I am also attaching a image. Thanks in advance.
In cocos2d the easiest way to accomplish this is to move your entire layer. If you're doing things in the normal cocos2d way, you have a class that subclasses CCLayer.
Within that class you can simply change its position and you'll get the desired effect.
In your example, since you want to move the "camera" to the right, you can simply move the layer to the left. for example:
-(void) centerOnBuilding {
CGPoint distanceToMove = ccp(-120, 0);
self.position = ccpAdd(distanceToMove, self.position);
}
That function, in a CCLayer, will have the effect of centering the camera 120 pixels to the right of where it was before. If you want to have a scrolling effect, do the same thing but run a MoveTo or MoveBy action in the layer.
what you have are two different coordinate spaces that should be independent from each other.
The ball should move in the "world" coordinate space, independently from the "screen" coordinate space. Of course that the "world" must be displayed on the "screen", so you must convert the "world" coordinate space to "screen" coordinate space, which in you case is a simple translation that can be done by adding an offset.
On iPhone, you can simply put all the "world objects" inside a UIView and move that 'UIView' left and right, while the world objects move relatively to the "world" inside that view, and the conversion from "world coordinate space" (the view coordinate space) to the screen coordinate space will be done for you when the view is drawn on screen.