Ok so I'm using Apples IOS Breadcrumb [Sample Code][1] .
I've a slider to the BreadcrumbViewController.xib called "lineWidth" and have a UIlabel which shows the value of the slider being changed called "sliderLabel". This I can do with relative ease.
Although now I want whatever number is in the sliderLabel to affect the CrumbPath lineWidth which at the moment is in CrumbPathView.m
{
CrumbPath *crumbs = (CrumbPath *)(self.overlay);
CGFloat lineWidth = MKRoadWidthAtZoomScale(zoomScale);
Now this can be changed to
CGFloat lineWidth = 200;
Or whatever.
so my question is how do I store the value of the sliderLabel located in the BreadcrumbViewController.xib (which is linked to the BreadcrumbViewController.h &.m) and retrieve the stored value (INT) in the CrumbPathView.m?
Im still learning so be nice.
Thank you in advance.
Related
In my Cocos2d js android app i have a UI button as
var button=new ccui.Button();
button.loadTextures(res.play_png,res.play_png);
button.setAnchorPoint(cc.p(0,0));
button.x=size.width/2;
button.y=size.height/2;
this.addChild(button);
The button loads and i am able to set it at any position on the screen.
But i am unable to change its width and height to specified number of pixels.
I want something like
button.width=100;
button.height=100;
I did not find any method to do that...
Any help how to accomplish this would be greatful.
Thanks.
The buttons i use are options like play,how to play,share etc..
So i have to position them in such a way that they dont overlap or get distorted for various screen resolutions...
How can i use the setScale method in this context???
We can make a call to a function that can scale the size of the UIButton by performing an action. In this case, the following code might help:
performScaleAction : function()
{
var duration = 0; // duration in seconds for performing this action
var scaleX = 0.5; // scale factor for X-axis
var scaleY = 0.5; // scale factor for Y-axis
var scaleAction = new cc.ScaleTo(duration , scaleX , scaleY );
this.runAction(scaleAction);
}
Now, call the above function in the following way:
this.performScaleAction();
this.addChild(button);
So, if the actual height of the image used for creating the UIButton is h, in this case, the new size as displayed on the screen would be h times scaleX . Same for the width of the UIButton.
Hope this helps!
I have a SKSpriteNode that moves with the accelerometer by using the following code:
-(void)processUserMotionForUpdate:(NSTimeInterval)currentTime {
SKSpriteNode* ship = (SKSpriteNode*)[self childNodeWithName:#"fishderp"];
CMAccelerometerData* data = self.motionManager.accelerometerData;
if (fabs(data.acceleration.y) > 0.2) {
[gameFish.physicsBody applyForce:CGVectorMake(0, data.acceleration.y)];
}
}
This works well however, the node (gamefish) moves off the screen. How can I prevent this and have it stay on the screen?
Try using an SKConstraint which was designed exactly for this purpose and introduced in iOS8:
Just add this to the setup method of the gameFish node. The game engine will apply the constraint after the physics has run. You won't have to worry about it. Cool huh?
// get the screensize
CGSize scr = self.scene.frame.size;
// setup a position constraint
SKConstraint *c = [SKConstraint
positionX:[SKRange rangeWithLowerLimit:0 upperLimit:scr.width]
Y:[SKRange rangeWithLowerLimit:0 upperLimit:scr.width]];
gameFish.constraints = #[c]; // can take an array of constraints
The code depends on whether you have added the gameFish node to self or to another node (something like a "worldNode"). If you have added it to self, look at the code below:
// get the screen height as you are only changing your node's y
float myHeight = self.view.frame.size.height;
// next check your node's y coordinate against the screen y range
// and adjust y if required
if(gameFish.position.y > myHeight) {
gameFish.position = CGPointMake(gameFish.position.x, myHeight);
}
For the bottom you can do a check of < 0 or whatever value you need.
What is the best way to display some value (that changes as the game runs) on the screen in iPhone SpriteKit? The only way I can think of is SKLabelNode, but it's probably not meant to be used like this and also I can't find any way to measure its width, which makes me unable to position it correctly (I want it to be in the bottom right corner). Thanks in advance :).
#Edit
My attempt at doing this:
SKLabelNode *someLabel = [SKLabelNode labelNodeWithFontNamed:#"Chalkduster"];
someLabel.text = some_string;
someLabel.fontSize = 30;
someLabel.fontColor = [SKColor blackColor];
someLabel.position = CGPointMake(some_int, 15);
[self addChild:someLabel];
The values of some_string and some_int change as the game runs, so someLabel is removed, someLabel.text and someLabel.position are re-assigned, and the label is added again. Yes, I am aware that this is a bad way to do this...
Unfortunately, SKLabelNode is your simplest bet, it's just not the most robust tool.
You just want to update its text and its position when you need to. Your code is correct, and if you want to get its actual size, then you would get the width of its frame.
update text:
someLabel.text = theNewText;
update position:
someLabel.position = theNewPosition;
get relative width
float widthOfLabelFrame = someLabel.frame.size.width;
additional alignment settings that might help (vertical baseline is the default):
someLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeRight;
someLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeBaseline;
Currently I have the following code to update the NSLevelindicator:
[self.headerIndicator selfFloatValue:heightValue]
Is there any way to animate the the level indicator between values when they update?
read this answer to animating sliders. i haven't tried it with level indicators but from the API docs it seems a viable option. the source code there can easily be adapt: NSSlider animation
I have tried this code and it works fine:
Swift:
self.CpuLevel.maxValue = Double(value)
If [[[self.headerIndicator] animator] setFloatValue:heightValue]; doesn't work, you must add the capability yourself. See NSAnimatablePropertyContainer.
You could write your own method, something like this to change the level. The math in there tries to keep the total "animation" time equal regardless of how big the transition is from the old value to the new (around 1 second with these values).
-(void)updateLevel:(NSNumber *) newLevelNum {
float currentLevel = self.level.floatValue;
float newLevel = newLevelNum.floatValue;
if (self.delay == 0.0) // self.delay is a float property set to 0.0 elsewhere
self.delay = .2 /(newLevel - currentLevel);
[self.level setFloatValue:currentLevel + .2];
if (self.level.floatValue - newLevel < .01){
[self performSelector:#selector(updateLevel:) withObject:newLevelNum afterDelay:self.delay];
}
}
HI all,
i am developing a puzzle game in iPhone using cocos2d.I need a progress bar (like uiprogress bar) to show the game progress time.But i can't find any good example...
can anyone tell me the way???
well....i get a better solution...here is my code
CCProgressFromTo *to1 = [CCProgressFromTo actionWithDuration:levelTimeLimit from:100 to:0];
timeBar = [CCProgressTimer progressWithFile:#"Bar.png"];
timeBar.type = kCCProgressTimerTypeHorizontalBarLR;
[timeBar setPosition:ccp(384,84)];
[self addChild:timeBar];
[timeBar runAction:to1];
there is a class called CCProgressTimer in latest version of cocos2d..
thanks
You can use a CCSprite that you set the width of using
yourSprite.scaleX = 0.5 //This goes between 0.0 and 1.0.
You will have to calculate the required width, percentage and scaleX-factor manually but its pretty simple. I did my fiend hp bar implementation like this:
-(void)decreaseHp:(float)hpIn {
self.hp = self.hp-hpIn; //Decrease HP by specified amount.
float p = (self.hp*100)/self.maxHp; //Calculate new hp percentage.
self.hpBar.scaleX = p/100; //Convert percentage to a factor between 0 and 1.
}
self is the Fiend object and hpBar is a simple CCSprite with anchor ccp(0,0).
You you don't want you progress bar to stretch, but move instead, you will have to mask it with something and update its position instead of scaleX.