Xcode 7 Swift 2 - Move button within a specific view area - button

I would like to move a button's position (X & Y) within a specific area of a view object.
What I've done so far is, I've added a view object on to my main screen and I've also added a button inside the view, so far OK.
Now, I managed to get the bounds of the view object and I know the size of my button say (50x50). So what I want is, when I click on the button, it should move to another random location but within the view object, it should not move outside the view object boundaries. Right now what happens in my button appears to be moving a portion of it outside the view object based on the random X & Y.
Any help will be appreciated.

I managed to get it as well. Here is how I did it:
I created a function which I pass the shape to and return the location that I want the piece to move to which is within the bounds of the view.
func randomizeLocation(shape: CGRect) -> CGPoint {
let randomX = arc4random_uniform(UInt32(gameBoard.frame.width - shape.width)) + UInt32(shape.width / 2)
let randomY = arc4random_uniform(UInt32(gameBoard.frame.height - shape.height)) + UInt32(shape.height / 2)
return CGPointMake(CGFloat(randomX), CGFloat(randomY))
}
Just in speaking about the x coordinate (left/right)
The logic here is that arc4random_uniform will return a random # between 0 and the given number (- 1).
But you don't want a number starting at zero because that would put the object off the edge of the screen to the left by 1/2. So, if you add 1/2 of the width to the overall function, you will never go off to the left side of the screen.
To keep from going off the right side of the screen you want a maximum number of the screen width minus 1/2 of the width of the object. To get this, you must specify the number you pass to the arc4random function as minus the whole width of the shape, because when you add back half, you are shifting by 1/2 of the width of your object on both sides (minimum and maximum return value).
So to use this function, take your object, lets say a UILabel and set its center to this functions result as such:
let myLabel = UIButton(type: .Custom)
myLabel.frame = CGRectMake(100, 100, 100, 100)
myLabel.center = randomizeLocation(myLabel.bounds)
And that it. Btw, I did not account for the -1 in the return b/c i figured the one pixel was too insignificant to bother with..

Related

Correct way of extending map boundaries without using bounds.extend(point)

I am using map bounds to display markers that fall within the current viewport. Upon zooming or panning the bounds are recalculated and the markers that fall within these bounds are redrawn, effectively hiding any not contained within the viewport.
I would like to do it so that the markers draw slightly out of the current viewport, however this would involve extending the bounds equally from all sides rather than using bounds.extend(point). Is this possible?
//I would like to extend this value in order to draw features that are slightly off the viewport
var bounds = map.getBounds()
//This is how I am currently extending the bounds, it works but I am unsure if it is the correct way.
bounds.b.b = bounds.b.b - 0.5
bounds.b.f = bounds.b.f + 0.5
bounds.f.b = bounds.f.b - 0.5
bounds.f.f = bounds.f.f + 0.5
//Determining whether the feature lies within the current viewport
var result = bounds.contains(Featurecenter)
center = null
//If the feature lies within the viewport
if (result) {
Feature.setMap(map) //Making the feature visible on the map
}
Don't use b, f and such, which are undocumented properties, instead use documented methods such as getNorthEast(), getSouthWest() then lat() and lng() when you need to extract coordinates from a bounds object.
I don't know of any other method to extend a bounds object than to pass new coordinates to it (see below).
The LatLngBounds object has an extend() method that you must use to achieve what you want.
We don't know of "how much" you need to extend the bounds; depending on that, and on the current zoom level when you want to extend your bounds, you will probably need to do some maths.
If you need to extend your bounds by a given (and constant) distance, whatever zoom level is currently set, I suggest you to read some other Q/A that explain how to do that. For example:
Google maps distance based on the zoom level
Google Maps V3 - How to calculate the zoom level for a given bounds

Capturing last location's object in a variable?

Right now I've got about a 10x10 grid of squares that the player can move 1 square at a time on.
When they hop to a square, I need an animation to play based on the sprite_index of the square they're jumping to and the one they just came from.
I've got the "jumping to" one sorted out.
in a collision event between the player and the square (other here being the square):
with(other){
if sprite_index = sGreenH {
instance_create(x,y,oGreenPlayerAni)
(also is there a better way to do the above? instead of spawning it ontop of what's there can I delete it/replace it THEN put something?)
So now I'm trying to get an animation to play from the square the character is leaving. I can do that with the player collision w/ square :
xx = xprevious and yy = yprevious
instance_create(xx,yy, someanimation)
problem there is that I can't customize which animation plays. There are 4 possible colors of animations to use for 4 diff color squares.
so I tried with the collision event in my square with the player making a variable like
if sprite_index = sGreen {
global.previousColor = 1
for each of my colors. and then in my player's collision event with the square again I have
if global.previousColor = 1 {
instance_create(xx,yy, oGreenHollowAni)
And then I get an error when I move.
Code square colliding with player (player on a square) : http://puu.sh/n9zCY/2f226b6d3c.png
Code player colliding with square : http://puu.sh/n9zK6/deac1a09f5.png
Error : http://puu.sh/n9zPj/ea84a9a943.png
I am not sure if I understand your question right. As far as I can see, you do always create a new instance when the player is moving?
If so, that is not good. When you create your 10x10 grid of squares I guess you create an array, in which you put the information which color square gets displayed, fe. array[x][y] = color.green ... you could then create an enum an define green = 1, blue = 2 ...
This array would be a global one.
From the player class you then check on which square you are currently and if you move you check on which square you will be. With these informations you can define the animation.
let me know if this was what you meant.
eric

How to get the negative position value in Group?

The dashed rectangle is the parent group and inside, there is a label. Its x is negative.
Now, what I want to do is relocation the outside group to the contents' top-left point and meanwhile the contents' move back to the outside group's (0,0) point. The result looks like everything keeps the same position as before.but in fact, both inside content and outside group is moved.
It is easy to realize in flash, however, in flex i got trouble.
The function "getRect" returns wrong values.it's never return the correct position the inside content is.(like the thumb shows,the position should be like [-70,50])
(Feel free to correct me because I'm not sure what you want to accomplish here)
If your Label (let's say it is called myLabel) is correctly located directly inside of your Group, simply calling myLabel.x will return the X-coordinate of the label compared to its parent (which is your Group here, so you should get -70).
Then if you want to move the label so it fits into your Group viewport, you have two solutions:
Either you manually set myLabel.x = 0 and myLabel.y = 0. In this case the label will actually be moved at the Group origin.
Either you retrieve the matrix of your label component to call its .translate(dx, dy) function. Using the matrix functions will modify the way your Label is displayed, but its position will remain unchanged (More information about that on this page).
Short answer: If you don't care about keeping the original position of your label, just set myLabel.x = 0 and myLabel.y = 0 and it should be moved correctly.

How to get actual position of qtoolbar?

I am just trying to get the actual x, y values or Qt::ToolBarArea area of my QToolBar.
Is there any function to get it so?
Check out QWidget::mapTo and associated functions. I reckon you want to do something like:
yourWidget->mapToParent(0, 0) - maps the top left corner of yourWidget (i.e. 0, 0) to the coordinate system of its parent widget.

3D, AS3, Flex - Convert degrees of rotation to visible height

I need to know what the visible height of a display object will be after I change it's rotationX value.
I have an application that allows users to lay out a floor in 3D space. I want the size of the floor to automatically stretch after a 3D rotation so that it always covers a certain area.
Anyone know a formula for working this out?
EDIT: I guess what I am really trying to do is convert degrees to pixels.
On a 2D plane say 100 x 100 pixels, a -10 degree change on rotationX means that the plane has a gap at the top where it is no longer visible. I want to know how many pixels this gap will be so that I can stretch the plane.
In Flex, the value for the display objects height property remains the same both before and after applying the rotation, which may in fact be a bug.
EDIT 2: There must be a general math formula to work this out rather than something Flash/Flex specific. When viewing an object in 3D space, if the object rotates backwards (top of object somersaults away from the viewer), what would the new visible height be based on degrees of rotation? This could be in pixels, metres, cubits or whatever.
I don't have a test case, but off the top of my head I'd guess something like:
var d:DisplayObject;
var rotationRadians:Number = d.rotationX * Math.PI / 180;
var visibleHeight:Number = d.height * Math.cos(rotationRadians);
This doesn't take any other transformations into account, though.
Have you tried using the object's bounding rectangle and testing that?
var dO:DisplayObject = new DisplayObject();
dO.rotation = 10;
var rect:Rectangle = dO.getRect();
// rect.topLeft.y is now the new top point.
// rect.width is the new width.
// rect.height is the new height.
As to the floor, I would need more information, but have you tried setting floor.percentWidth = 100? That might work.
Have you checked DisplayObject.transform.pixelBounds? I haven't tried it, but it might be more likely to take the rotation into account.
Rotation actually changes DisplayObject's axis's (i.e. x and y axes are rotated). That is why you are not seeing the difference in height. So for getting the visual height and y you might try this.var dO:DisplayObject = new DisplayObject();
addChild();
var rect1:Rectangle = dO.getRect(dO.parent);
dO.rotation = 10;
var rect2:Rectangle = dO.getRect(dO.parent);
rect1 and rect2 should be different in this case. If you want to check the visual coordinates of the dO then just change dO.parent with root.

Resources