What is the right way to zoom the camera out from a scene view? - coronasdk

I have a scene where my gameplay happens. I'm looking for a way to slowly 'zoom-out' so more and more space becomes visible as the time passes. But I want the HUD and some other elements to stay the same size. It's like the mouse wheel functionality in top-down games.
I tried to do it with sticking everything into display groups and transitioning the xScale, yScale. It works visually of course but game functionality screws up. For example I have a spawner that spawns some objects, its graphics gets scaled but spawner still spawns the objects from its original position, not the new scaled position..
Do I need to keep track of every X, Y position I'm using during gameplay and transition them too? (Which is probably possible but too hard for me since I use a lot of touch events for aiming and path creating etc.) Or is there an easier way to achieve this? Please please say yes :P
I'm looking forward for your answers, thanks in advance! :)

The problem is you are scaling your image, but not your position coordinates.
You need to convert from 'original coordinates' to 'scaled coordinates'
If you scale your map size to half, you should scale the move amounts by half too.
You need to 'scale' your coordinates in the same amount your image is scaled.
For example lets assume your scale factor is 0.5, you have an image:
local scaleFactor = 0.5
image.xScale = scaleFactor
image.yScale = scaleFactor
Then you would need to scale your move amounts by this factor
thingThatIsMoved.x = thingThatIsMoved.x + (moveAmount * scaleFactor)
thingThatIsMoved.y = thingThatIsMoved.y + (moveAmount * scaleFactor)
I hope this helps, I am assuming you have a moveAmount variable and updating the position on the enterFrame() event.

Related

What's the difference of setScale and size of SKSpriteNodes in SpriteKit and Swift?

I was trying to set the size of a SKSpriteNode from a PNG image (OK with that, just a:
test = SKSpriteNode(imageNamed: "myImage")
But when it comes to resize it, I'm not sure if I should use test.setScale or test.size.
What's the difference and when should I use each of them?
setScale():
Sets the xScale and yScale properties of the node. These two parameters are the scaling factor that multiplies the width (for xScale) and the height (for yScale) of a node and its children.
Code sample:
spaceship.setScale(0.50) // decreased scale to it's half size
size():
Expresses the dimensions of the sprite (width and height), in points.
Code sample:
spaceship.size = CGSizeMake(100.0, 70.0) // Re-size the dimensions to these values
When do you use setScale and when do you use size?
Usually setScale is used to increase or decrease the sprite about the scale factor, look for example the SKAction.scaleTo used to make a zoom in or zoom out.
size is frequently used because you can express in points the exactly values you want to do to your sprite.
You should use size when you want to change the specific size of a SKSpriteNode without affecting the size of any children of that sprite node.
Scaling a sprite node not only changes the size (scale) of the node, it also scales all that sprite node's children appropriately (proportionally) as well.
If a sprite node has no children, then there's no practical difference between size and scale.
Note: I find it helpful to use size when it doesn't matter so that I know that a scale of 1.0 is always the default size - handy for animating using scaleTo instead of scaleBy and back again to default size.
If you are using a spriteNode, I would use .setScale, or .xScale or .yScale , because those are specifically for sprites. .size can also be used for the size of labels, and text views in UIKit.
Lets say you want the sprite to be 2x bigger:
theNode.xScale = 2.0
theNode.yScale = 2.0
Note that they have to be floats, not Integers
So to sum it up, .setScale is for spriteNodes and images in general, and .size is for a vast amount of things that aren't images, like labels and more. So if you are using a sprite, use .setScale. If you are resizing a block use .size.
If you believe that this is the right answer, please mark it as so so it helps anyone else with this issue. Thanks!
I alway resize using the .size property since I believe a node's physics body does not automatically scale when you use the .setScale / .xScale / .yScale. If you scale the node, you will need to redo the size of the physicsBody.

How can I adjust this positioning to follow a "zoom-to-point" effect?

This is my 3rd day of googling and tinkering with my code, and I'm hopeless, I honestly need help, so please bear with me here.
Explanation
I'll start with explaining the basic concept. There is a 'character' in the middle of the screen, always perfectly centered. Under that layer is a big square, which I will refer to as the canvas. As you move around, the game is actually just simulating the movement, a 'virtual camera' if you will, so the character doesn't actually move, the canvas just moves behind it.
My Problem
In this game, the camera needs to be able to 'zoom' in and out, so to do this, I adjust the size of the canvas and everything else depending on the zoom value. (1 is default) The width and height of the canvas is always 8000 * the zoom value. This is fine, however, I am having major difficulty positioning the canvas so that it remains in the same relative position as you scale in and out - or rather a 'zoom to point' or 'zoom from point' effect. I simply cannot create the math for this.
The Code
local oldScale = g.scale;
local newScale = n.scaleTest.Value;
g.scale = newScale;
g.x = g.x + (g.x * newScale);
g.y = g.y + (g.y * newScale);
g.scale represents the zoom value, everything will be adjusted to this size.
g.x and g.y are the coordinates of the canvas. The math in the code is completely incorrect.
n.scaleTest.Value is my temporary variable for the newly set scale. It can change at any time.
Thoughts / Other Important Information
I can retrieve the AbsolutePosition and AbsoluteSize of the canvas as well. I have considered implementing the distance from the canvas's edges to the character into my calculations, but I am not completely sure how to do this.
Thank you for any help, I would sincerely appreciate it, I am very stuck and don't know who to ask.

Rotate UILabel On Clockface

Is it possible to rotate UILabels or similar about a clockface?
For example, if I had an array of numbers from one to twelve, it would render as such in a circular manner. However, with a touch event I could move the labels about the face of the clock i.e. I could move 12 in a rightward motion until it reached 6, with all other numbers remaining in their positions.
I have considered UIPicker, but that follows a 'top to bottom' approach. I need a solution to move the label about a point while maintaining the order of the other labels.
It's simply a matter of opposing rotation transforms. Rotate the clock face while at the same time rotating all the subview numbers the same amount in the opposite direction.
Recommended:
1 Clock without small & Big handle (As Image) set it in ImageView.
2 Another two image view one for Small hand another one for Big hand.
3 Rotate the hands(UIImageView) by using the below code as you needed.
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
UIImageView *smallHand = set your smallHand image view here;
UIImageView *bigHand = set your bigHand image view here;
// Small hand
CGAffineTransform smallHandTransform = CGAffineTransformRotate(self.transform, (DEGREES_TO_RADIANS(1)));
// Big hand
CGAffineTransform bigHandTransform = CGAffineTransformRotate(self.transform, (DEGREES_TO_RADIANS(1*0.60)));
// Apply rotation
smallHand.transform = smallHandTransform;
bigHand.transform = bigHandTransform;
I knew that question was rotating the label, if you need it apply this transform on UILabel.
I hope somebody will make use of this idea. Good luck :)
Here is the example project

Can SKPhysicsJoints be used in a scrolling SpriteKit game?

This seems to be a major oversight by Apple, but since SKPhysicsJoints have their anchor points set in Scene coordinates, this makes doing any kind of scrolling game impossible.
To simulate a camera in SpriteKit you create a WorldNode which contains all of the gameplay elements, and then pan that around the scene. Unfortunately, doing this causes the Scene coordinates of every object in the game to change on every frame as you pan the world around. In turn, this breaks the joint anchor points, and things go berserk.
There isn't even a way to change the joint's anchor point, so I don't even have a way of just updating the coordinate every frame. It would seem that using SKPhysicsJoint in a scrolling game is not an option.
Does anyone know of a way around this?
Ok, I think I figured out what was going on, and I was totally incorrect in my original assumption. The reason my anchor points looked incorrect is because the [convertPoint toNode] call was returning me Scene coordinates that were incorrect. After several hours I realized it was off by exactly half the screen dimensions. My Scene has an anchorPoint of (0.5, 0.5), but this screws up the conversion values. So, if I simply offset the point by width/2, height/2 it's correct:
GPoint pt = CGPointMake(anchorWorldX, anchorWorldY);
pt = [gGameScene convertPoint:pt fromNode:gGameWorld]; // convert to scene coords, but it's WRONG
pt.x += scene.size.width * scene.anchorPoint.x; // this properly adjusts the value to be correct
pt.y += scene.size.height * scene.anchorPoint.y;
SKPhysicsJointPin* pin =[SKPhysicsJointPin jointWithBodyA:hinge.physicsBody bodyB:door.physicsBody anchor:pt];

Getting the same coordinates from UIScrollView whether it's zoomed or not

Is it possible to get the same coordinates from UIScrollView whether it's zoomed or not.
That is, For example, consider a plain screen of 320.0F and 480.0F.
Tap on a point; view will give me something like (60.0F, 80.0F).
Zooming-in or zooming-out on the view so that it will have either bigger or smaller zoom scale but making sure the zoomed area to contain the point that was tapped which was (60.0F, 80.0F) according to previous zoom scale.
Tap on the point again, the view will give me different coordinate value.
The Thing is, I want to have the same coordinate value whether the view is zoomed or not. The idea is simple. I want to show images zoomed & interactive without changing its coordinate. Considering an UIScrollView applied of the idea with its height of 1.0 and width of 0.66, I think there would be some pros programming this way, when making an interactive app without using OpenGL, cocos2d or whatever 3D engines out there.
Do you guys have any idea if it's supported or not? Either case, please don't wait any second to reply. Thanks
You can calculate it by yourself using content size as follows,
x = (original_Width/ width_after_zooming) * point.x
y = (original_height/ height_after_zooming) * point.y

Resources