Sprite not moving in a fixed path for all devices using PathModifier in Andengine - andengine

I have to move these Sprites (balls) like this in andengine. I am able to do it using path modifier by taking co-ordinates but the main problem is how to make it to work for all resolution tabs? Because whatever co-ordinate I am taking is fixed for the device but not exactly fitting for others. My path is like this
final Path path = new Path(12).to(0, CAMERA_HEIGHT - 440).to(CAMERA_WIDTH - 285, CAMERA_HEIGHT - 440)
.to(CAMERA_WIDTH - 280, CAMERA_HEIGHT - 435).to(CAMERA_WIDTH - 275, CAMERA_HEIGHT - 430)
.to(CAMERA_WIDTH - 270, CAMERA_HEIGHT - 425).to(CAMERA_WIDTH - 265, CAMERA_HEIGHT - 420)
.to(CAMERA_WIDTH - 260, CAMERA_HEIGHT - 415).to(CAMERA_WIDTH - 255, CAMERA_HEIGHT - 410)
.to(CAMERA_WIDTH - 250, CAMERA_HEIGHT - 405).to(CAMERA_WIDTH - 250, CAMERA_HEIGHT - 400)
.to(CAMERA_WIDTH - 250, CAMERA_HEIGHT - 200).to(CAMERA_WIDTH - 250, CAMERA_HEIGHT - 200);
help

In andengine if you target for one resolution then it will automatically compatible to all other resolution devices.But you have to implement your code in camera width and camera height values format.
So, convert all these hard coded values to camera width and camera height format.It will work.

Related

How to set line width on template clipping

I have some PdfTemplate and I want to clip its shape to some path. I know how to do this, but the clipping line is always the same (probably 1 px) and I want to be able to change it. Is there any way to do this? Half-measures, like resizing template will not do the trick.
Piece of code:
PdfTemplate template = contentByte.CreateTemplate(100, 200);
template.MoveTo(0, 0);
template.LineTo(50, 50);
template.LineTo(50, 0);
template.LineTo(0, 50);
template.SetLineWidth(5);
template.Clip();
Image img = Image.getInstance(RESOURCE);
template.Add(img, 0, 0);
SetLineWidth() obviously doesn't work. Both C# and Java answers will help.
Edit: In this scenario we have img in triangle. What if we want to clip this image like this, but without changing coordinates (I would like to set line width on 10):
template.LineTo(45, 45);
template.LineTo(45, 0);
template.LineTo(0, 45);
Problem #1: You never stroke the path, hence it is never drawn. First try this:
PdfTemplate template = contentByte.CreateTemplate(100, 200);
template.MoveTo(0, 0);
template.LineTo(50, 50);
template.LineTo(50, 0);
template.LineTo(0, 50);
template.SetLineWidth(5);
template.Clip();
Image img = Image.getInstance(RESOURCE);
template.Add(img, 0, 0);
template.Stroke();
Problem #2: You are using your clipping path for two different purposes.
To cut out a shape when adding an Image.
To draw the path.
That doesn't look right. I'm not sure if every PDF viewer will actually stroke that path as you clearly use that path to clip content.
I would write this code like this:
PdfTemplate template = contentByte.CreateTemplate(100, 200);
template.MoveTo(0, 0);
template.LineTo(50, 50);
template.LineTo(50, 0);
template.LineTo(0, 50);
template.Clip();
template.NewPath();
Image img = Image.getInstance(RESOURCE);
template.Add(img, 0, 0);
template.MoveTo(0, 0);
template.LineTo(50, 50);
template.LineTo(50, 0);
template.LineTo(0, 50);
template.SetLineWidth(5);
template.Stroke();
The first time, you use the path as a clipping path. It doesn't make sense to define a line width for a clipping path: the path defines the shape that needs to be cut out.
The second time, you use the path to stroke the borders of a shape. The lines that make these borders have a width. Note that you're only drawing three lines. You may want to close the path!
This is also strange:
template.LineTo(45, 45);
template.LineTo(45, 0);
template.LineTo(0, 45);
This doesn't draw a triangle!
These lines should be corrected like this:
template.MoveTo(0, 45);
template.LineTo(45, 45);
template.LineTo(45, 0);
template.LineTo(0, 45);

Finding the position of a button. CGRectMake

I have a button and I'm using CGRectMake to position it like this:
btn5.frame = CGRectMake(211, 280, 109, 60);
I guess 211 and 280 are the coordinates of the button. So how can I modify that code if I want the button to be near the bottom left corner.
Yes, you are correct. The frame of the button defines where it is located on its parent view. So btn5 will be in the position (x = 211, y = 280) and will have the dimensions (width = 109, hight = 60) if you use this code:
btn5.frame = CGRectMake(211, 280, 109, 60);
The general expression is:
btn5.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
If you want to change the position of the button to the bottom left corner, you can do like this:
btn5.frame = CGRectMake(0, self.view.frame.size.height - 60, 109, 60);
If you want to add some space between the button and the corner, and also change the size of the button:
CGFloat x = leftPadding;
CGFloat y = self.frame.size.width - desiredHeight - bottomPadding;
btn5.frame = CGRectMake(x, y, desiredWidth, desiredHeight);
For more information, check Apple's View Geometry and Coordinate Systems guide.
In CGRectMake(211, 280, 109, 60) you are defining a frame for your button.
The first two parameters are the x & y coordinates location of the upper-left corner of your button (in the parent view's coordinate system)
The second two parameters are the width and height of your button.
If you want your button in the absolute bottom left corner of the parent's view, then you should set the x coordinate to 0 (i.e. 0 pixels from the left edge of the parent) and the y coordinate to the parent view's height - 60 (where 60 is the height or your button). If you want to leave some margin around your button, then adjust the x and y accordingly.

Draw continuously gradient line in cocos2d

I have searched and searched and not found anything that works for me. How can I draw a continuously line with a gradient in cocos2d-iphone? I have tried CCRibbon but then I get gaps, and I have tried to draw multiple aligned lines in the draw method but with different alpha vales, but my draw method does not react to setting the alpha (it is always 100% alpha, see my draw method here under). How can I do this please?
- (void)draw {
glEnable(GL_LINE_SMOOTH);
glColor4ub(0,255,255,50);
ccDrawLine( ccp(0 - 5, 0 - 5), ccp(200 - 5, 300 - 5) );
}
Thank you
Søren
I am not sure whether this is what you need:
- (void)draw {
static GLubyte alpha = 0;
static int step = 1;
alpha += step;
if (alpha == 255 || alpha == 0) {
step = -step;
}
// You gonna need these two lines to use Alpha channel
glEnable(GL_BLEND); // disabled by default
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LINE_SMOOTH);
glColor4ub(0, 255, 255, alpha);
ccDrawLine(ccp(0, 0), ccp(200, 300));
}
For more information about GL_BLEND and glBlendFunc you can refer to OpenGL ES 1.1 Reference.
From the question I'm thinking you want, for example, a thick line that runs from left to right and a gradient that runs from the top of that line to the bottom. If so, then this is how I did it. I created a 1 x 6 png image that had a vertical gradient with an alpha built into it. Then when I wanted to create a line, I would call my drawLine method in my layer.
- (void) drawLine: (CGPoint)origin withEnd:(CGPoint)end
{
CCSprite *wall = [CCSprite spriteWithFile:#"WallGradient.png"];
float distance = sqrt(powf(origin.x - end.x, 2) + powf(origin.y - end.y, 2));
float rotation = (180/M_PI) * acosf((origin.x - end.x) / distance));
[wall setScaleX:distance];
[wall setRotation: rotation];
[wall setPosition: origin];
[self addChild:wall];
}

CGImageCreateWithImageInRect causing distortion

I'm using CGImageCreateWithImageInRect to do a magnifying effect, and it works beautifully, except when I get close to the edges of my view. In that case, clipping causes the image to be distorted. Right now I grab a 72x72 chunk of the view, apply a round mask to it, and then draw the masked image, and a circle on top.
When the copied chunk is near the edge of the view, It winds up smaller than 72x72 because of clipping, and then when it's drawn in the magnifying glass it gets stretched out.
When the touch point is close to the left edge, for example, I would like to create an image where the left part is filled with a solid color, and the right half contains part of the view that's being magnified. Then apply the mask to that image and add the overlay on top.
Here's what I'm doing now. imageRef is the image being magnified, mask is a round mask, and overlay is a circle to mark the edges of the magnified region.
CGImageRef subImage = CGImageCreateWithImageInRect(imageRef, CGRectMake(touchPoint.x - 36, touchPoint.y - 36, 72, 72));
CGImageRef xMaskedImage = CGImageCreateWithMask(subImage, mask);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextConcatCTM(context, xform);
CGRect area = CGRectMake(touchPoint.x - 84, -touchPoint.y, 170, 170);
CGRect area2 = CGRectMake(touchPoint.x - 80, -touchPoint.y + 4, 160, 160);
CGContextDrawImage(context, area2, xMaskedImage);
CGContextDrawImage(context, area, overlay);
I solved this by using CGBitmapContextCreate() to create a bitmap context. Then I drew the captured area into a smaller area of this context, and created an image from it with CGBitmapContextCreateImage(). That was the missing piece of the puzzle.

Arc / Circle animation in iphone

How would I animate a circle on the iPhone so that the arc starts at "0 degrees" and ends at "360 degrees"?
Advance Thanks,
Sat
You need to read the Quartz 2D Programming Guide's section on arcs. (I am assuming you are creating an app with the Cocoa Touch API, not a web app.) You also need to know how to set up a custom animation. You will have to create a custom UIView or CALayer to do the drawing, and create a property (arc degree) that can be animated with a CAAnimation object. Alternatively, you can control the animation using an NSTimer instead. You pretty much have to have a grasp of these classes (and others) to pull this off.
Here you can find a great sample code about circle animation:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/24152-draw-animated-circle-iphone-using-core-graphics.html
you should read the documentation that Felixyz provided and if you want an example of how to animate the circle have a look over the MBProgressHUD at this link link text. The loader has two modes one with a UIViewActivityIndicator and a progress indicator (a filling circle that it is animated from 0 to 360 degress) i think the last mode is what you want.
the fallowing code is from copy/paste from that implementation that animates the circle:
- (void)drawRect:(CGRect)rect {
CGRect allRect = self.bounds;
CGRect circleRect = CGRectMake(allRect.origin.x + 2, allRect.origin.y + 2,
allRect.size.width - 4, allRect.size.height - 4);
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw background
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); // white
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.1); // translucent white
CGContextSetLineWidth(context, 2.0);
CGContextFillEllipseInRect(context, circleRect);
CGContextStrokeEllipseInRect(context, circleRect);
// Draw progress
float x = (allRect.size.width / 2);
float y = (allRect.size.height / 2);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); // white
CGContextMoveToPoint(context, x, y);
CGContextAddArc(context, x, y, (allRect.size.width - 4) / 2, -(PI / 2),
(self.progress * 2 * PI) - PI / 2, 0);
CGContextClosePath(context); CGContextFillPath(context);
}
but read the documentation first! hope it helps