SKLabelNode not displaying the right CGPoint - ios

I have a problem in my game and i am trying to find out what it is and it is about the position of a SKSpriteNode and i made a SKLabelNode to display the position so i could see what is going wrong. I did:
testlable.text = "\(leftObstacle.position)"
testlable.zPosition = 50
testlable.fontSize = 60
It displays the position of (00,00) when it is in the top left corner or outside of the screen not ever the right position and it isn't ever at (00,00) so what should i do to make it display the right position?

The position parameter is the position of a node relative to its parent node - so if leftObstacle is a child of another node in the scene that would explain why it always looks like it is at 0,0. In this case report the position of its parent (or its grand-parent etc.) to get what you need.

Related

Move object left and right in ARKit session

How to move an object left and right in ARKit scene without changing its Y-axis, if its in the air then it doesn't change that and just move left and right in screen?
This will be a very general example, I could get more specific if you make your question more detailed or give an example of what you are trying to do.
Lets say you want the object to just move on it's x-axis and you had an object of type SCNNode called selectedObject, you could do something like this
selectedObject.position = SCNVector3Make(value, selectedObject.position.y, selectedObject.position.z)
In this case, you can provide the value you want for the x axis position while maintaining the position the object has on the y and z axes.

ScrollView in SpriteKit

I have found a few other questions and answers similar to this, but none of them quite work perfect for me.
My vision is to have a horizontal scrollable view at the bottom of the screen where I can scroll through what will look like cards in a hand. Ideally, I could eventually make the card in the middle scaled up a bit and give it a highlighted look to show the user which card is selected. Even better would be if I could figure out how to keep the scroll view resizing to fir the number of sprites (cards) in the view.
Anyways, I am still very new to XCode and Swift, so it is hard for me to take what I find and change it. But, I am hoping to learn fast.
What I understand so far is that a UIScrollView could overlay the scene and with a moveable spritenode I could scroll through the view. The view would then translate the coordinates somehow to the SpriteKit Scene to move the sprites that will look like they are in the view. I think that's how it works. Any help would be great. I am pretty stuck. <3
You have to make your own logic that takes place in touchesMoved() using a global/member variable.
Unfortunately, a lot of gamedev and SK is math and logic.. You have to come up with your own problems and solutions.. There is no manual because the possibilities in programming and Swift are endless :)
Moving the cards:
Basically, you compare each touch location to the last one, and this becomes a "delta value" that you can use to perform actions.
Example, if I touch in the center of the screen, my touch location is 0,0 (or whatever your anchorpoint is set to). If I move my finger right, then I'm now at say 25, 0... This creates a "delta value" of +25x.
With that delta value, you can perform various actions such as moveBy for all the cards... so if I have a deltaX of +25, then I need to move all of the card nodes to the right (by a certain amount that you will determine according to your preferences). If I have a deltaX of -25, I move the cards to the left by a certain amount.
Where you do the actual moving is up to you--you could put a function in update() or touchesMoved() that constantly moves the cards a certain direction at a certain rate of that deltaX value..
Ok that was a mouthful... Maybe this will help:
for touch in touches {
myGlobalDeltaX = myDeltaXFunc(currentTouch: touch)
myMoveFunc(cards: allTheCards, byDeltaX: myGlobalDeltaX)
- You can search on how to make a Delta function, but it really is just the same thing from Algebra.
- myMoveFunc can be something as simple as iterating through all of your card nodes then running .moveBy on them at the same time.
Middle detection:
To detect which card is in the center, you would put in touchesEnded() or update() a call to check the name / identity of the node in the center of the screen... so something like
// `self` here refers to your GameScene class' instance, which is just an `SKScene` object
let centerX = self.frame.midX
let centerY = self.frame.midY
let center = CGPoint(x: centerX, y: centerY)
let centerNode = self.nodes(at: center)
You would obviously want to change centerX and centerY to wherever it is you want the middle card to be :) Right now, this is just in the dead-center of the screen.
Once you have a centerNode, you would then just need to do whatever function you have created to "select" it.
let selectedCard = centerNode
mySelectionFunc(middleCard: selectedCard)
This may look like a lot, but I drew out the steps to make understanding it a bit easier.. You can do all of this in one line if desired.
mySelectionFunc(middleCard: self.nodes(at: CGPoint(x: self.frame.x, y: self.frame.y)))
Hope this helps some!

make subview descend from "behind" another subview

Within a UISrollView, I have several programmatically-added subviews representing nodes in a tree:
Each node's frame includes the node itself plus the line connecting it to its parent. (I did this to facilitate animation of the line with the node.) In the picture below, the frame is drawn for one of the nodes:
When the user taps on one of the nodes, two child nodes are "born". I'd like to animate this by having the child nodes descend down from behind the parent node. My basic animation code is:
- (void)descendFromParent
{
// Do nothing if this is root node
if (!self.parent)
return;
// Move node to parent location
self.frame = CGRectMake(self.frame.origin.x + self.parent.nodeFrame.origin.x - self.nodeFrame.origin.x,
self.frame.origin.y + self.parent.nodeFrame.origin.y - self.nodeFrame.origin.y,
self.frame.size.width, self.frame.size.height);
// Animate the move back to original location
[UIView animateWithDuration:1.0
delay:0.0
options:0
animations:^{
self.frame = self.trueFrame;
}
completion:nil];
}
(nodeFrame is a frame containing just the circular part of the view.)
The problem of course is that as the child node is descending, it (especially the line) is visible on top of and above the parent node:
I've tried a lot of different ways to make this work -- using CGContextClip, etc. -- but none of them work, mainly because drawRect: isn't called during the animation.
How can I make this work?
I think your problem is not how to arrange the views in the correct view hierarchy, but the following:
After you have arranged the child disks behind the parent disk, you want them to slide down, and while they are sliding, the edge that connects their centers should have first zero length (when all 3 disks are at the same place), and should then be extended until it reaches its final length in the end.
If this is the case, one possible solution would be:
Lets call the initial x,y center coordinate of one disk (0,0), and the final coordinate (X,Y). Using the usual animation code, it is easy to move the child center to (X,Y) in time t.
Now assume you have an additional image view that shows the edge along the diagonal. In the end position, the center of the "edge view" is at (X/2,Y/2), and its size is (X,Y). If this view is placed behind all others, it will connect the two disks at their final position.
The animation consists now of 1) changing the center property from the initial position (0,0) to (X/2,Y/2), and 2) changing the scale of the view (using its transform property) from zero to the final size, also in time t.
This should do it.
You may want to look at the UIView methods
-insertSubview:belowSubview:
-insertSubview:atIndex:
-sendSubviewToBack:
In your case, you can either send the two subviews to the background with -sendSubviewToBack: after you have added them. Or you can just add them with -insertSubview:atIndex: and provide 0 as the index.
You can specific an index for a subview, try inserting the nodes you want to animate at index 0 and have the blue nodes at a higher index.

How to check if bottom of a SKSpriteNode is inside the current visible scene

I've been looking at Figure 7-3 in this sprite kit documentation: https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Actions/Actions.html but it has left me pretty confused. They seem to give the same name to multiple things e.g., camera/character.
I am in the default SKScene, MyScene class. In initWithSize, I create a SKNode *myWorld, just like the documentation suggests. I then have a series of methods that add my background images to myWorld. Scrolling that works just fine, but what I want to do is stop the vertical scrolling when the bottom of the images in myWorld reach the bottom of the scene. For the life of me, I cannot figure out how to refer to the bottom of myWorld. For the bottom of the scene, I simply do
CGPoint sceneFarBottomSide = CGPointMake(0, -self.size.height/2);
where self.anhorPoint is set to [0.5, 0.5].
How do I refer to the bottom of myWorld?
The edge of myWorld is whatever you set it to be. In other words, myWorld is a node which isn't itself a view or a sprite. It's simply an object that contains sprite or shape children (for example, SKSpriteNodes or SKShapeNodes). When you are adding your sprites to myWorld, keep track of their position. Then use their position to define the "size" of myWorld. You can use this size information along with myWorld.position to know when the (bottom) edge of myWorld is coming up.
It ended up being extraordinarily easy to resolve. Thanks Andrey for pointing me to that Apple documentation on the Adventure game, that's what really tipped me off and cleared up some of my understanding. Here's the few lines of code to get the behavior I desired:
// Move world
if (monkeyPosition.y > 0 && monkeyPosition.x > 0) {
[myWorld setPosition:CGPointMake(-monkeyPosition.x, -monkeyPosition.y)];
} else if (monkeyPosition.y > 0 && monkeyPosition.x < 0) {
[myWorld setPosition:CGPointMake(0, -monkeyPosition.y)];
}

How to move an UIView along a curved CGPath according to user dragging the view

I'm trying to build a interface that the user can move his finger around the screen an a list of images moves along a path. The idea is that the images center nevers leaves de path.
Most of the things I found was about how to animate using CGPath and not about actually using the path as the track to a user movement.
== Update. Better description ==
I need to objects to be tracked on the path even if the user isn't moving his fingers over the path.
For example (image bellow), if the object is at the beginning of the path and the user touches anywhere on the screen and moves his fingers from left to right I need that the object moves from left to right but following the path, that is, going up as it goes to the right towards the path's end.
== End update
This is the path I've draw, imagine that I'll have a view (any image) that the user can touch and drag it along the path, there's no need to move the finger exactly over the path. If the user move from left to right the image should move from left to right but going up if need following the path.
This is how I'm creating the path:
CGPoint endPointUp = CGPointMake(315, 124);
CGPoint endPointDown = CGPointMake(0, 403);
CGPoint controlPoint1 = CGPointMake(133, 187);
CGPoint controlPoint2 = CGPointMake(174, 318);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, endPointUp.x, endPointUp.y);
CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPointDown.x, endPointDown.y);
Any idead how can I achieve this?
You can take a mathematical function that draws a graph, and then move along this trajectory object.

Resources