Make a Defender-like "world" with SKTileMapNode - sprite-kit

Defender is an ancient game that presents a virtual world, a horizontal scroller, in a loop. The camera view only portrays a portion of the world at any given time. The player can fly in both directions around this world, continuously.
How could this be achieved with an SKTileMapNode Layout of the world?
My first thought is to have two instances of a SKTileMapNode that illustrates the whole world, and abut them in space where and when required as the player flies around the world.
Here's a map of the Defender world highlighted at the top of the screen:

The minimap in the screen shot you show indicates it is not endlessly scrolling, it’s just really long, with only a small part visible at any time.
Doing this with SKTilemapNode would involve one SKTilemapNode and adding tiles only for the ground, which you could then attach physics bodies to. The background could be a single image or multiple images to create a parallax effect or you could go with a black colour background and stars created with SpriteKit Particles.
An alternate approach would be to use two background images and position them programmatically so as one image scrolls offscreen, it is moved left or right (depending on which way the user is scrolling) to appear adjacent to the other background image, giving the appearance of an endless scrolling background.

Related

Four gameobjects that are supposed to be walls on the edges of the screen are not scaling with the screen size

I am trying to make a game on mobile unity2d and I am having trouble getting this to work.
There are four game objects I am using. These represent the walls. These four gameobjects are placed on the edges of the screen and the player can only move within this space. However, when the device changes from an iPhone to an iPad, these walls do not appropriately appear in the correct spot. The iPad will show what the game would look like if running on an iPhone, with the top and bottom walls appearing closer to the middle rather than the top of the screen where they should be. How could I get these objects to stick to the edges and resize themselves based on the device's screen size?
iPhone:
iPad:
How am I supposed to get these walls to stay on the edges of the screen no matter what screen size it is?
I have only tried using scripts that scale the entire games resolution but even that doesn't work.

Loaded SKTileMap from scene disappear

I'm trying to make a game and I need to render a tileMap that the player can move but also modify its size.
First I created it programmatically, filled it randomly and it worked perfectly fine. As a result I designed a custom tileMap in the scene editor and I used it in code so that the player is able to move it or change its size and there I have a problem.
The tileMap is a 48x48 tileMap but if the intersection between the 24 and 25 column touch the left side of the screen, the whole tileMap (even the part on screen) disappear and if I bring it back on the screen (by selecting the invisible position where it was) and drag it back on the right it reappears.
The only solution that I found is to design the whole tileMap programmatically but this will take quite a long amount of time. Has anyone found how to tackle this issue ?
(changing shouldCullNonVisibleNodes and transparency has no effect)

swift physicsbody is not matching up to image size

I have two nodes, player and platfrom. Both have a physicsBody around them. My player is still and the platform scroll by an SkAction. when the player reaches about half-way on top of the platform it falls threw it.
player.physicsBody=SKPhysicsBody(rectangleOf:player.frame.size)
player.physicsBody?.isDynamic=true
player.physicsBody?.allowsRotation=false
player.physicsBody?.affectedByGravity=true
platform.physicsBody=SKPhysicsBody(rectangleOf:platfrom.frame.size)
platform.physicsBody?.isDynamic=false
the platfrom set up is.
plat.size=CGSize(width:(self.frame.width)*2,height:(self.frame.height)/3)
plat.anchorPoint=CGPoint(x:0,y:0)
plat.position=CGPoint(x:0,y:0)
plat.zPosition = 2
addChild(platform)
I tried using this instead but player still falls down
plat.physicsBody=SKPhysicsBody(rectangleOf:CGSize(width:platform.width,
height: platform.frame.height))
I want the nodes to act like a solid object, in that they don't overlap each other when the player moves. Also it there an alternative method I can use. I don't really like working with physics.
An alternate to working with physics is to use the recently added to iOS 10 SKTilemapNode to create the background. Each tile can be tagged with data to indicate if the tile is ground or not. You then focus on how the player moves relative to the tiles. There are currently not a lot of examples to show this working, the WWDC 2016: What's new in SpriteKit shows this briefly. Other resources include a top down example which shows at least how to build the background tilemap and some background info on why physics engines don't provide the best experience for platformers. Kenney's is a good source of tiles for platformer artwork.

Problem regarding CCFollow and CCParallaxNode

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).

Scrolling a game environment in various directions

How does one create the game "area" for a scroller game?
How does one then put various obstacles with collision detection along this scrolled environment.
I want to try out a project which will allow the user to scroll to a certain direction in order to progress through the game.
How does one map the objects within the environment and then move what I guess is the "camera", the view of the environment.
Thanks
The trick is that there is no "area". The only bits that exist are what's under the camera (the view you currently see) and a small surrounding area giving you time to prepare more world in the direction you are moving..
Your world coordinates need to be defined as do the starting coordinates for the view. You use tiles to create the view - at its simplest that is 9 tiles, one you are currently "on" and one in each direction. If you look at the keyboard numberpad you are "on" the 5. If you move a little to the top right you are displaying parts of tiles 8, 9, 5 & 6. At that point you would create new tiles in case you move further. As you leave tile 5 you would probably release tiles 4, 1 & 2. Nine tiles may not be the optimal number of course.
If doing this with UIViews (probably not the high-performance choice) you are probably going to define one big view that can handle all the tiles and tile them onto the view (add and remove subviews), setting the large view's frame to define your camera position. As you move you change the frame to move your camera, when you need to shuffle tiles you move both the tiles and the frame to recenter giving room to move further within the coordinates of your view.
Collision detection is pretty simple since you define your own dimensions (the thing representing "you" in this world) and objects in your view have dimensions you can check against. CGRectIntersectsRect might be the simplest function to use but if you have irregularly-sized views it will get more complicated.
This answer about implementing a cyclic UIScrollView is a similar idea but it only handles scrolling on one direction.
This is a pretty common topic and if you google you will find a lot of sample code and tutorials around.
From the game logic side:
All your objects (lets call them gameobjects) should have a coordinate (x and y position) in your game world. You will keep all your gameobjects in a list. Your player object will be a gameobject too. Usually your "camera" position will be relative to your player objects position. I.e. the player will always be in the center of the screen. To determine the current "screen" position of your objects you will just subtract the camera position from your objects "world" position. Collision is usually made with simple rectangular overlap checks. You give all your objects a width and a height attribute and do your collision checks using x, y, width and height.
From the display side:
If you want to display many objects (i.e. Player, Enemies, Obstacles and so on) the best way to implement something like this is to use an OpenGL View. In this view you can display all Objects as Textures that are mapped to Polygons. You can use a library such as cocos2d which already has all of the code to achieve this easily.