I'm currently trying to code some falling stars in my game, and they seem to disappear once they move out of the screen. But I'm not sure if swift is actually automatically deleting for me, or if they're still lingering around.
I'm starting them at the top of the screen and using physicsBody to use gravity to bring them down.
star.physicsBody = SKPhysicsBody(rectangleOf: star.size)
I know apple documentation says "Later, if the sprite is removed from the scene or is no longer visible, SpriteKit can delete the texture data if it needs that memory for other purposes."
https://developer.apple.com/documentation/spritekit/skspritenode/getting_started_with_sprite_nodes
but I find that kind of vague and just want some confirmation that because I can't see my star sprite anymore on my screen, I can also assume that it's being removed.
(and not just clumping up somewhere off screen)
No - it's not automatically removed.
By 'removed from scene' the SK documentation doesn't mean that if you can't see it anymore, it means that the 'RemoveFrom...' method was called on the sprite in question.
If the sprite isn't visible, then the game engine won't draw it and could optimise memory by removing it's texture, but the sprite is still being tracked and colliding and bouncing off objects etc.
The SK Scene extends infinitely in all directions and your device's screen is simply a moveable window onto the scene. If you decide in your game that a sprite which moves offscreen is no longer needed, then you need code to detect this and then issue 'RemoveFromParent' against the sprite.
Related
I created a bunch of non-AI animations for enemies that do basic back.forth or up/down motions. Everything was working perfect until I started working on the Boss of the level. The boss has his own tag "Boss" and enemies have their own tag as well. Anyway, the problem is when I click start, every enemy leaves the game board. I can see them animated above the game board still doing their routines.
Any clue as to why this happened and how to fix it? I'd really, really hate to have to scrap all the enemies and start from scratch...
I used the Animation tool inside unity.
Extra Note: I created an EMPTY and moved all of my enemies into that Empty object to clean the hierarchy panel up. The animations were fine before this.
************** Currently Resolved **************
Whew! Okay, so apparently Unity doesn't like it when you move your animated stuff into an empty AFTER being animated. I FIXED them by simply removing them from the EMPTY that I had placed them in. However, I'd still like to know why this is. So any useful resources, links, manuals, personal insight/observations or anywhere I can read up on this would be appreciated!
Here is an explanation what happened:
The moment you dragged them all into the empty GameObject I guess this object probably wasn't placed on 0,0,0 in the Scene.
So Unity automatically changed all the local position values of your enemy items to fit the current position offset to the empty object.
Result: In the editmode they don't change their actual global position in the scene but their local position. This is supposed to happen if you just want to organize stuff.
However, now when you start the game and the animations are played, the animators change all the local positions to whatever is stored in your animations.
Result: all objects jump back to their original localPosition which had an offset to the empty GameObject.
To solve this make sure the empty GameObject is at position 0,0,0 and optimally has rotation 0,0,0 and scale 1,1,1 before you drag anything into it.
Easiest way to achieve that is by clicking reset in the empty objects Transform component before starting to drag stuff into it.
When the camera is obscured, the AR system loses track of the position of gameobjects and player should recalibrate the surface to make the game objects appear again.
Players can accidentally obscure the camera anytime, so this is not desirable.
Is there a way to prevent this from happening, so that the gameobjects reappear in their original positions after the camera is un-obscured?
Here's a video describing the issue
Thanks!
-Jake
I've never had this behavior, my guess is you need a larger space with more track-able distinguishable objects.
It's not got enough to to realise that it's looking back at the same place.
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.
I would like to move a tile with drag and drop in Unity 2D. The tile is a sprite. The scene is an 'Unblock me' or 'Blocked in' like gameplay.
Because the tiles in real life correspond to physical objects it seemed be to a good idea to model them with colliders and rigidbody. The border of the table surrunded with invisible colliders. I hoped these will constrain the moves of the tiles realistic, when the player moves them.
Then I implemented a simple (mouse based) drag and drop behavior which is worked perfectly except the moved tile penetrates to other tiles and the border, and sometimes jumps over them. Then I learned if I am overriding physics by explicitly setting transforms position (which I do exactly in my drag and drop implementation), this will happen. OK I accept, I should set only forces, ect. on rigidbody never directly the position.
Now the question:
I am stuck here. I still want to drag and drop like user experience, and some realistic visual result. When in the real life a player moves a tile, it seems it is "glued" to its finger. How can I achieve this (ot at least similar) with just applying forces? Any suggestions or point similar existing sample/blog code?
(I know as a backup plan I can omit all the physics and constrain the tile positions by code, and create some tweens to move the tiles. Is the real solution (what I am asking for) so complicated I should vote on this backup plan?)
Edit
According to comments I've added a video:
There is nothing wrong with the way you are manipulating your dragging. The beauty of developing is being able to do things in your own way. If it works for your game, then don't fret.
Now, i recommend:
Create a new physic material. Assets > Create > Physic Material
Set your new physic material inspector settings both to 0
Attach the physic material to all your wall object colliders. This should allow for your object being dragged to move smoothly against the walls without chopping.
Do a check to see if your mouse is over another collider. If so, then stop the movement in that direction.
Since your movements seem to always be on a single axis, on collision, tell your object to snap to the edge of the wall object. You know the Wall position and scales, also you have the position and scales of the object being dragged. with that you can write a function that will offset it to the correct position when the collision occurs.
Let me know if any of that works out :P
I have a simple project built with Cocos2D and Chipmunk. So far it's just a Ball (body, shape & sprite) bouncing on the Ground (a static line segment at the bottom of the screen).
I implemented the ccTouchesBegan/Moved/Ended methods to drag the ball around.
I've tried both:
cpBodySlew(ballBody, touchPoint, 1.0/60.0f);
and
ballBody->p = cgPointMake(touchPoint.x,touchPoint.y);
and while the Ball does follow my dragging, it's still being affected by gravity and it tries to go down (which causes velocity problems and others).
Does anyone know of the preferred way to Drag an active Body while the physics simulation is going on?
Do I need somehow to stop the simulation and turn it back on afterwards?
Thanks!
Temporarily remove the body from the space.
If you want the object to have inertia when you release it, that's a different story. The cleanest way is to attach a fairly stiff spring between the ball and a temporary sensor body that moves under the control of your finger. When you let go with your finger, the ball will retain whatever kinematics it had while you were dragging it. Be sure not to remove the ball from the space in this case.
You aren't updating the velocity of the object when you aren't using cpBodySlew(). That is why it falls straight down.
A better way to do it is to use a force clamped pivot joint like the official demos do to implement mouse control. http://code.google.com/p/chipmunk-physics/source/browse/trunk/Demo/ChipmunkDemo.c#296