A shortcut to get the point of previous frame plus its width? - objective-c

Many times when i am programing a UI , i have to put some view after the previous view, so to get the previous value i am calculating its x/y position ,add to this its width/height, and get the desired frame to the next view ,
newX=previous.frame.origin.x+previous.frame.size.width;
newy=previous.frame.origin.y+previous.frame.size.height;
Is there a short way to get that value of a view ,in one word ?
When you have to do that many times , it becomes a real headache every time again .

You can use C functions declared in CoreGraphic.h:
newX = CGRectGetMaxX(previous.frame);
newy = CGRectGetMaxY(previous.frame);

You can define a macro
#define NextX(frame) (frame.origin.x+frame.size.width)
#define NextY(frame) (frame.origin.y+frame.size.height)
and then you can use it like
newX=NextX(previous.frame);
newY=NextY(previous.frame);

Related

How to place half-block slabs in Minecraft with MakeCode

This is a bit of a long-shot. I really don't know where to ask this question.
I've been trying out CodeConnection + MakeCode with Minecraft and I haven't been able to figure out if there is correct way to place half-slabs at 0.5 step y axes increments.
I tried using a line between 2 points, but it left gaps between each slab.
If I try moving up 0.5, then it rounds it up to 1, and again leaves gaps.
It appears that all of the builder functions seem operate at a resolution of 1 block. However in-game I can obviously place slabs in 0.5 block increments to make stairs etc.
Blocks only exist at integer coordinates. Half slabs that exist in the top half of their space are still at a full integer coordinate. They just have a BlockState value of bottom=top (or top_slot_bit=true on Bedrock, represented by the integer value 8 as a bitflag, eg: 0b1... where the . bits are the integer representation of what type of slab (wood, stone, quartz...)).
What you're looking for is this widget, under Blocks:
You can set the block and then an integer representation of the desired data value (see the wiki on data values) in the numerical slot. This widget can then be dragged into the (block) portion of any block widget:
You'll probably have to some variable fiddling to get the data value to swap back and forth as you need it to, but that should solve the hurdle you've been facing.

Random 4x4 2D NSArray Objective-C

I'm trying to create a Minesweeper game.
I have a 4x4 set of buttons equally spaced in main.Storyboard.
My plan is to create a random array which places a 0 or * in the 1st/2nd/3rd/4th arrays. I would do this by using the arc4Random method.
With the remaining blank cells, I then have to check how many mines there could be for the 8 (potential) squares around the cell/button. This would be governed by the boundary conditions (0,0 to 3,3).
Once this is set up, I would then set the background and number label to the same colour. I could then write an if or else statement to change the colour after each button is pressed.
I'm quite struggling how to start this off and actually write this. Can anyone please give me some advice please?
Well,
you can get a boolean like this.
bool hasMine = arc4random() % 2;
this will give you 50% chance to get a bomb... if you want less bomb, increase the value (3 will give you 2 bomb free square, for one with a bomb, etc..)
then a "" or a "*" like this;
NSSString * value = hasMine ? #"*" : #"" ;
then it's just a matter of a for loop to populate your arrays.
for the sake of performance, I wouldn't use a n x n nested array but a single arrray of nxn size (in your case a array with 16 value). Then I will set a tag for 0 to (nxn -1) to each button based on its position, and on click I'll get the tag of the pressed button and retrive the value of the object at this position in the array

Random letters instead of numbers in dynamic text AS 2.0

So I'm trying to program a game using flash, and it's my very first time and I can't get something to work.
In the game, a ball will float across the screen and if you click on it you get 2 points. Except when I test it, the first time I click on the ball I get the letters 'eoceeeo' and if I click the ball again I get the letters 'eeoS'. The dynamic text is on a layer with the first frame having the AS of
var _root.score = 0;
gameScore.text = _root.score;
The dynamic text has a varible of _root.score and a name of gameScore
The floating ball has the AS of
on(release) { _root.score+=2; _root.gameScore.text = _root.score; }
If you click on your gameScore dynamic text field, you can scroll down to its Variable property and set that as _root.score. That way, you do not have to call gameScore.text = _root.score every time the score changes - it will simply update automatically.
Also, if you remove the var from in front of _root.score = 0, it will be easier for ActionScript to handle. Perhaps, you are casting the score variable as an integer, and the dynamic text field is having trouble displaying it as a string of characters. This can also be solved with String(_root.score) and score.toString().
That should make your code a bit less complex, and help for you to identity your random letters problem, which can't be solved specifically with the information you have here. Hope that helps!

Label displays an incorrect value

I have this code:
In .h file:
#interface ViewController : UIViewController
{
float costBonus9;
}
In .m file:
costBonus9 = 123456789;
NSNumber *numbercost9 = [NSNumber numberWithFloat:costBonus9];
NSString *formatcost9 = [formattercost stringFromNumber:numbercost9];
Label9.text = formatcost9;
But Label9 displays "123456792".
What's the problem?
The problem is that you're using a single precision floating point number, i.e. a float, to represent 123456789. A float stores it's value as a combination of a significand (sometimes called the 'mantissa') and an exponent, not unlike scientific notation:
s * 2^e
The size of the significand determines how precisely you can represent a given value. A single precision float has a 24-bit significand, so you get 24 significant bits. Unfortunately, you can't fit 123456789 in 24 bits, so you get the best approximation of that value that does fit, which as you've found is 123456792.
If you want an exact value, use a type that can store the value exactly. You could use a double, or if you'll always be storing integers you'd probably be better off with a long int.
I would use a debug, and a break point to make sure that formatcost9 really has what you think it has.
To add a breakpoint in xcode, click the gray bar that seperates the Navigator (the list of classes and such) from the canvas (where the code shows), make sure you click next to the line of code you want to check. I would click at numbercost9, then run the program. When it reaches the breakpoint it will pause. The debug menu will be shown, click the arrow that points down towards a thick bar. This will move you through the code line by line.
There should be two boxes in the debug menu, one of the right (this is your console.) and one on the left, which shows variable values. Use this one to figure out the value of formatcost9.

Two NSTextFields with interdependent widths in autolayout

I’m trying to put together what seems to be a simple case of two NSTextFields with dynamic width and fixed spacing in between. I cannot figure out an effective way to do so though.
I’m looking to get something like this:
The blue boxes are the NSTextFields. When more text is entered into one, it should grow and thus make the other one shrink, maintaining the lead space, trailing space and the spacing in between the fields. The first one should take the priority if both of the fields have too much text. Each field will also clearly have a maximum and a minimum possible width it can reach.
How would I go around handling this, preferably utilising IB autolayout as much as possible?
It seems to me that all of constraints you mentioned directly translate into interface builder --
First view has width >= something.
First view has width <= something
Same for Second view.
Space between views is fixed.
Second view wants to be as small as possible (have its width at 0) but this has lower lower priority than the previous constraints and lower priority than inner content size constraints.
The code I had to add to my view controller, after applying the constraints as per the ilya’s answer:
In controlTextDidChange (_controlWidthConstraint refers to the fixed width constraint of the input; it’s probably 0 by default for the second input):
// Get the new width that fits
float oldWidth = textControl.frame.size.width;
[input sizeToFit];
float controlWidth = textControl.frame.size.width;
// Don’t let the sizeToFit method modify the frame though
NSRect controlRect = textControl.frame;
controlRect.size.width = oldWidth;
textControl.frame = controlRect;
_controlWidthConstraint.constant = controlWidth;
The key lies in invalidating the intrinsicContentSize for the text field when text is input.
You can check a sample project here, to get you on the right track.