Making a progress bar change a UILabel? - iphone

I am attemping to try and make a progress bar change a UILabel every .15 of the progress bar to a different phrase could anyone help me out with this?
I have tried using "if" statements i.e if prog.progress = .15 UIlable.text = #"Fire!" but everytime I do a higher progress number it just replaces the UILabel with that text.
Does anyone know an easier method for doing this?
Thanks for your help.

You could do something like a lookup of a string based on the progress. There are lots of ways to do this, simplest maybe to understand is a if/then/else tree.
if (prog.progress < .15)
label.text = #"msg1";
else if (prog.progress >= .15*1 && prog.progress < .15*2)
label.text = #"msg2";
else if (prog.progress >= .15*2 && prog.progress < .15*3)
label.text = #"msg3";
Warning, didn't compile above code, but something along those lines should work.

DavidNeiss is correct. Make sure you are using == and not =. = is the assignment operator and == is used to test. Also, checking if a value is exactly .15 probably isn't that realistic if it is continuously raising and your program might skip that catch. you should be using < and > to say if it is this range, then do this. If it is in this range, do this.

Related

Scratch - How can I make something like a for loop?

In a traditional programming language, I would do:
for (int i = 0; i == 5; i++) {
code here
}
If I needed to loop something five times, I could just use the block repeat 5. But if I want to do something like this:
for (int i = 0; i == 5; i++) {
if (i >= 3) {
//Repeat piece of code after 3rd loop
}
//Repeated 5 times normally
}
How can I achieve something like this?
Something like this, just create variable and put it inside repeat loop:
Actually, there is already one!
It is called the foreach block, and works just like a for loop.
It was removed from the block listing in the early Scratch 2.0 beta, and not many people know about it, but here is a working example on the Scratch website: For each v in number. Note that it doesn't use %m.list internally (i.e. it doesn't show a dropdown of all the variables), so you have to change the JSON of the project with something like ScratchEdit (or a text editor) if you want to pick a variable other than v.

How would I use recursion to create an array of extra change($100, 50, 20, 10, 5, 1) MATLAB

How would I correctly make a recursive call within every if-statement to get the change of money? Im specifically focusing on the "change" variable.Thanks
TEST CASE 1-------------------------------------------------------------------------------
<>> [change,flag] = makeChangeRecursive(2,100)
change =
50
20
20
5
2
1
flag =
1
My code is the following
function [change,flag] = makeChangeRecursive(cost,paid)
if extra > 0
flag = true;
elseif extra == 0
change = 0;
flag = true;
return
elseif cost > paid;
flag = false;
change = [];
warning('That''s not enough to buy that item.');
return
end
if extra >= 100
change = [change; makeChangeRecursive(cost,paid - change )];
paid =paid-100;
elseif extra >= 50
change = [change; 50];
paid =paid-50;
elseif
This continues for all dollar values.
Let's take a look at your first case:
if extra >= 100
change = [change; makeChangeRecursive(cost,paid - change )];
paid =paid-100;
elseif ...
The first time we call your function, the variable change doesn't have anything in it. In fact, it will never have anything in it at the beginning of the function call because you don't pass it in as a parameter or give it a value prior to this line. So putting change on the right-hand side of the assignment will give you an error.
But that's okay, because that's not what you want to do anyway. You want to build change up from the beginning.
In addition, change is a list of values. We want to pass the recursive calls a single value, paid after updating its value.
Let's build this up step by step:
if extra >= 100
If this is true, we want subtract 100 from the amount paid (what we pass in to the recursive call) and add 100 to our list of change. Let's do the first part:
paid = paid - 100;
As I said, we want to update paid first because we're going to use this value in the recursive call, which happens next, along with adding our new change value to the list:
change = [100; makeChangeRecursive(cost, paid)];
elseif ...
And so on for the remainder of the change values. I'm sure you can take care of the rest of them now by yourself.
I also noticed that you didn't assign a value to extra. This might have been just a cut-and-paste error, but you need to make sure that you have that at the beginning of your function.

if statement float not returning results Unity3d JS/US

I have an if statement that is meant to check a float that increases over time (It's a timer) the current code is this
if (Day.DateTime == 1080){
ClientsTaken = Random.Range(MinNoOfClients, MaxNoOfClients);
GameObject.Find("Canvas").GetComponent(Brothel).Money += (ClientsTaken *(Cost * GirlMorale));
ClientsSeen += ClientsTaken;
Debug.Log("Clients taken = " + ClientsTaken);
}
The timer is definatly hitting 1080 but nothing is happening. I also tried 1080f but had the same result.
Any help would be greatly appreciated, thanks.
Comparing floating point variables this way is not recommended due to "floating point imprecision" - the way the floating point variables are internally stored on PC.
Instead, you can use Mathf.Approximately(), like this:
if (Mathf.Approximately(Day.DateTime, 1080.0))
{
...
{
I would change the check to Day.DateTime >= 1080. When dealing with floating point numbers you may not land directly on an integer value.

Logical indexing of fields within a structure

I have a structure like so:
Basis.FieldsBasisType.fieldsBasisComponents
There are ~13 components to each basis, including 6 asset class IDs.
So, for example
fieldnames(Basis.SalaryIncrease) =
'Constant'
'AWeight'
'AAssetClassID'
'ATimeLag'
'BWeight'
'BAssetClassID'
'BTimeLag'
'CWeight'
'CAssetClassID'
'CTimeLag'
'DWeight'
'DAssetClassID'
'DTimeLag'
'EWeight'
'EAssetClassID'
'ETimeLag'
'FWeight'
'FAssetClassID'
'FTimeLag'
'cap'
'floor'
Now what I want to do is select all unique asset classes used in any basis. I am really struggling to make this neat though, currently I am using:
basisNames = fieldnames(Basis);
requiredSeries=[];
for i = 1:size(fieldnames(Basis),1)
requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).AAssetClassID)];
requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).BAssetClassID)];
requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).CAssetClassID)];
requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).DAssetClassID)];
requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).EAssetClassID)];
requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).FAssetClassID)];
end
requiredSeries = unique(requiredSeries)
Which is really ugly in my opinion. I want to do some kind of string compare to find 'AssetClassID' within the fields, so something like:
field = fieldnames(Basis.(basisNames{1}));
strfind(field,'AssetClassID');
And then use that cell array to logically index 'field' and just grab the data from 'AssetClassID' fields. But I am stuck on making that work.
~cellfun('isempty',strfind(field,'AssetClassID'))
gets me the logical index, how do I apply that to fields and then use it to get values.
Any ideas would be appreciated, I feel there should be a neat way of doing it and I am missing something. Hardcoding those fieldnames seems short sighted as a solution.
#
Edit: I hate myself.
Sorry folks, I came up with a working variant like moments after posting this, apologies for wasting anyones time!
basisNames = fieldnames(Basis);
for i = 1:size(fieldnames(Basis),1)
field = fieldnames(Basis.(basisNames{i}));
field = cell2mat(field(~cellfun('isempty',strfind(field,'AssetClassID'))));
for j = 1:size(field,1)
requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).(field(1,:)))];
end
requiredSeries = unique(requiredSeries)
end
I was missing a necessary cell2mat earlier which caused the inability to get it to bloody work. Anyway, I'd always like to hear improvements to that but otherwise you can shut this down.
Sorry folks, I came up with a working variant 30 mins or after posting this, popping it down as an answer as per Michelle's suggestion.
basisNames = fieldnames(Basis);
for i = 1:size(fieldnames(Basis),1)
field = fieldnames(Basis.(basisNames{i}));
field = cell2mat(field(~cellfun('isempty',strfind(field,'AssetClassID'))));
for j = 1:size(field,1)
requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).(a(1,:)))];
end
requiredSeries = unique(requiredSeries)
end
I was missing a necessary cell2mat earlier which caused the inability to get it to bloody work. Anyway, I'd always like to hear improvements to that but otherwise you ignore this entirely :)

condition statements involving coordinates

I need help with making an if/then statement which depends on a movie clip being between a certain set of coordinates for the rule to work. Here is the code I tried to use:
if (honey1.x >=165 <=231.x;
honey1.y >=295 <=330.y;) {
honeyOne = true}
}
I haven't had much luck finding a lot of help on condition statements beyond very simple or unrelated notes. If anyone knows how to make a statement like this work, I would be most appreciative.
A method to test that a number is between two bounds might look like this.
bool IsBetweenInclusive(int value, int lower, int upper)
{
return value >= lower
&& value <= upper;
}
That's easily extendable to a point structure
bool IsBetweenInclusive(Point value, Point lower, Point upper)
{
return IsBetweenInclusive(value.X, lower.X, upper.X)
&& IsBetweenInclusive(value.Y, lower.Y, upper.Y);
}
If your coordinate system is in a double or float kind of number space, you'll need to do work to account for rounding.