hit test with for loop - iphone

i have 10 bricks, whenever my ball hit one of them i want to remove that brick. (brick breaker)
That is what i tried
for (int bri=1; bri<11; bri++) {
// NSObject *brickimg = [NSObject stringWithFormat:#"brick%d", bri];
// if (CGRectIntersectsRect(ball.frame, brickimg.frame)) {
// [UIImageView stringWithFormat:#"brick%d", bri].hidden = YES;
// }
}
and also
UIImageView *brickimg = [UIImageView stringWithFormat:#"brick%d", bri];
but no chance.
How is the correct Syntax ?

How about calling stringWithFormat: on the correct class, which is NSString?

Related

CCSprite doesn't go invisible -iphone

in .h file
CCSprite *backwheels;
in .m file
backwheels = [CCSprite spriteWithFile:#"wheels_back.png"];
backwheels.position = ccp(400,120);
[self addChild:backwheels];
-(void) showGameOver {
backwheels.visible = false;
}
but when Game is Over backWheels still appears on scene..? !
any help ?!
NOTE:i have synthesized backWheels too,but still doesn't work for me.
I can give you a quick fix. This is not the best approach, the best one is to find out what exactly causes such a behavior, but i can't do that without seeing the rest of your code. Anyway this is how you can access backwheels sprite in showGameOver method. When you create the sprite make it this way:
backwheels = [CCSprite spriteWithFile:#"wheels_back.png"];
backwheels.position = ccp(400,120);
backwheels.tag = 100; // whatever integer value you wish
[self addChild:backwheels];
Then you retrieve it in showGameOver :
backwheels = [self getChildByTag:100];
backwheels.visible = false;
I believe it's gonna work.
You could always just change the opacity of the sprite i.e.
-(void) showGameOver {
backwheels.opacity = 0.0f;
}
And then when you want it to reappear change it to
backwheels.opacity = 1.0f;
Use Remove Child :
[self removeChild:backwheels cleanup:YES];

Access NSMutableArray created in viewDidLoad on button tap

In my app I have an animation that plays when the user taps a button. In the IBAction method for the button tap, I am having it create a NSMutable Array, load the images into the array, then cycle through the images.
This results in a fair amount of lag between the button tap and the animation playing, but every tap after that is fine since the array is already created with the images.
I have tried placing the array creation and image loading in the vieDidLoad method, but for some reason the IBAction method (where the call to cycle through the images is) cannot access the array. How would I make the array available to it?
- (IBAction)tap {
NSMutableArray *anim = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:#"0001.png"], ...(x30)... nil];
type.animationImages = anim;
type.animationDuration = 1.0;
type.animationRepeatCount = 1;
[type startAnimating];
}
Define NSMutableArray *anim into your UIViewController .h file. It will make it available to global to access anywhere into your .m file.
- (void) viewDidLoad
{
anim = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:#"0001.png"], ...(x30)... nil];
}
- (IBAction)tap
{
type.animationImages = anim;
type.animationDuration = 1.0;
type.animationRepeatCount = 1;
[type startAnimating];
}
Define NSMutableArray *anim in .h file ie class member variable
in viewDidLoad method defin like this:
anim = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:#"0001.png"], ...(x30)... nil];
Use like this in button event:
- (IBAction)tap
{
type.animationImages = anim;
type.animationDuration = 1.0;
type.animationRepeatCount = 1;
[type startAnimating];
}
Try this simple code may be it help full for you.
Array allocate one time when you tap first time.
if(!anim)
{
NSMutableArray *anim = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:#"0001.png"], ...(x30)... nil];
NSLog(#"array allocate");
}else
{
NSLog(#"array already allocated");
}

Tapping button to go through each frame of animation

I am trying to create an app where when you tap a button it will go to the next frame in an animation.
I have 8 image files, and when I press the button I want the 1st image to display, and when i press the button again, I want the 2nd image to replace the 1st image and so on.
I was thinking something like:
-(IBAction)buttonPressDoStuff:(id)sender {
imageThing.image = [UIImage imageNamed:#"image1.png"];
imageThing.image = [UIImage imageNamed:#"image2.png"];
imageThing.image = [UIImage imageNamed:#"image3.png"];
imageThing.image = [UIImage imageNamed:#"image4.png"];
}
and somehow making this all work consecutively with each press.
I am fairly new at objective c, so any help would be much appreciated.
Can anyone throw up some sample code to do this?
Let's think about this. If you want to do something sequentially, that sounds like the job of an array. So what do you think about this:
In your .h file, add these instance variables:
NSMutableArray* picturesArray;
NSInteger counter;
And now in your .m file, in your class' init method:
//this loop will fill your array with the pictures
for(int idx = 0; idx < NUMBER_OF_PICTURES; idx++) {
//IMPORTANT: this assumes that your pictures' names start with
//'image0.png` for the first image, then 'image1.png`, and so on
//if your images' names start with 'image1.png' and then go up, then you
//should change the 'int idx = 0' declaration in the for loop to 'int idx = 1'
//so the loop will start at 0. You will then need to change the condition
//to 'idx < (NUMBER_OF_PICTURES + 1)' to accomodate the last image
NSString* temp = [NSString stringWithFormat:#"image%i.png", idx];
UIImage* tempImage = [UIImage imageNamed:temp];
[picturesArray addObject:tempImage];
}
and in your buttonPressDoStuff: method:
//this method will move to the next picture in the array each time it is pressed
-(IBAction)buttonPressDoStuff:(id)sender {
if(counter < [pictureArray count]) {
imageThing.image = [picturesArray objectAtIndex:counter];
counter++;
}
}
Your init method should look something like this:
- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self) {
//do setup here
for(int idx = 0; idx < NUMBER_OF_PICTURES; idx++) {
NSString* temp = [NSString stringWithFormat:#"image%i.png", idx];
UIImage* tempImage = [UIImage imageNamed:temp];
[picturesArray addObject:tempImage];
}
}
//it is important that you return 'self' no matter what- if you don't,
//you will get the 'control reached end of non-void method' warning
return self;
}

i have a problem in UIImageView's animating

-(IBAction) change {
self.imageView.animationImages = myImages;
self.imageView.animationDuration = 2;
if(self.imageView.isAnimating == NO){
[self.imageView startAnimating];
NSLog(#"if bool = %d", self.imageView.isAnimating);
}
else {
self.imageView stopAnimating];
NSLog(#"else bool = %d", self.imageView.isAnimating);
}
}
hello, i'm studying iOS programming.
but i have a question.
i have a button and when i click the button, then this method will be called.
first i click the button, then this code will start the if statement. that's what i want.
i click the button again, i think that will execute the else statement.
but it always execute the if statement only.
why is that?
i really don't know why is that. please help me
I think setting the properties like animationImages or animationDuration will stop the animation, so that by clicking, you every time stop and then just after (re)start it in the if part. Try setting these two properties outside the action method you wrote, and just let the if/else sequence.
-(IBAction) change {
// set these two anywhere else
//self.imageView.animationImages = myImages;
//self.imageView.animationDuration = 2;
if(self.imageView.isAnimating == NO){
[self.imageView startAnimating];
NSLog(#"if bool = %d", self.imageView.isAnimating);
}
else {
self.imageView stopAnimating];
NSLog(#"else bool = %d", self.imageView.isAnimating);
}
}

Hiding or moving SegmentContoller

Hello I've tried for 3 weeks to solve this issue and it stumps me. What i am trying to do is create a 3 part segment from an array, display it in a view in a certain position, then remove it from view when the "OFF" flag is set. Every thing works except the removal of the segment. It will even commuticate with (pickOne) and display the segment letters in a label. What i can't get to work is either of the two: setHidden:YES, or removeAllSegments. Any help would be appreciated. Here is my code.
- (void) showSegment {
int x = 192;
int y = 212;
int w = 125;
int h = 25;
SegUnit1 = #"A";
SegUnit2 = #"B";
SegUnit3 = #"C";
threeSegs = [NSArray arrayWithObjects: SegUnit1, SegUnit2, SegUnit3, nil];
segSize = [NSArray arrayWithArray:threeSegs];
UISegmentedControl *heightSC = [[UISegmentedControl alloc] initWithItems:segSize];
if ([segmentState_height isEqualToString:#"ON"]) {
NSLog(#"segmentState_height = %#",segmentState_height);
heightSC.frame = CGRectMake(x, y, w, h);
heightSC.segmentedControlStyle = UISegmentedControlStyleBar;
heightSC.selectedSegmentIndex = -1;
[heightSC addTarget:self
action:#selector(pickOne:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:heightSC];
[heightSC release];
} else if ([segmentState_height isEqualToString:#"OFF"]) {
NSLog(#"segmentState_height = %#",segmentState_height);
[heightSC setHidden:YES]; // NSLog showing "OFF" but segment will not hide.
[heightSC removeAllSegments]; // NSLog showing "OFF" and segment is suppose to dismantle and does not.
}
}
I know now that i have to "not" create and remove in the same function, and was given a tip on correcting this but I don't know how to use the tip.
here is what was suggested.
Well, your method is a little confused, since you are trying to both create and hide at the same time. So you might consider splitting that up into separate methods.
In general, it will be along these lines:
Code:
if ([self theControlProperty] == nil)
{
UISeg... *theControl = [[UISeg alloc] ....];
[self setTheControlProperty:theControl];
...
}
if (shouldHideTheControl)
{
[[self theControlProperty] setHidden:YES];
}
Any help would be appreciated.
The problem you have is that you're creating a new UISegmentedControl instance every time that method is called. The first time through, you create an instance and add it as a subview to your view. This apparently works fine, as it should. Then the method returns, and you no longer have any easy way to refer to that instance that you created. When you re-enter -showSegment, you create a different instance, and then hide and/or destroy it. This different instance has no effect whatsoever on the instance that you gave to the view.
What you need to do is make heightSC an instance variable. Add it to the interface declaration in the header file, then initialize it only once, and hide or modify it as needed subsequently. The key point is that you need to have a reference to the instance of the UISegmentedControl which is being drawn on the screen, a reference that lives outside the method itself that you can use the second, third, fourth, etc time you call that method.
Try using the remove segments in your button choice method pickOne. This takes it outside the showSegment method and matches the users desired action to make the change and clear off the buttons.
- (void) pickOne:(id)sender {
UISegmentedControl* userChose = sender;
if( [userChose selectedSegmentIndex] == 0 ){
your first button operation;
[heightSC removeAllSegments];
}
if( [userChose selectedSegmentIndex] == 1 ){
your second button operation;
[heightSC removeAllSegments];
}
if( [userChose selectedSegmentIndex] == 2 ){
your third button operation;
[heightSC removeAllSegments];
}
}
I tried this and got the results I was looking for. Thanks goes to Mythogen and BrianSlick I just need to check and make sure there are no leaks. Now that will be a task.
Does anyone know if I need the second [heightSC release]; ?
// .h
# interface ------ {
UISegmentedControl *segmentPicked;
}
|
#property (nonatomic, retain) UISegmentedControl *segmentPicked;
// .m
|
#synthesize segmentPicked;
|
if ([self segmentPicked] == nil) {
UISegmentedControl *heightSC = [[UISegmentedControl alloc] initWithItems:segSize];
[self setSegmentPicked:heightSC];
[heightSC release];
heightSC.frame = CGRectMake(x, y, w, h);
heightSC.segmentedControlStyle = UISegmentedControlStyleBar;
heightSC.selectedSegmentIndex = -1;
[heightSC addTarget:self
action:#selector(pickOne:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:heightSC];
[heightSC release];
}
if ([segmentState_height isEqualToString:#"OFF"])
{
[[self segmentPicked] setHidden:YES];
} else {
[[self segmentPicked] setHidden:NO];
}
[yourSegment removeFromSuperview];
?