The idea is: I have a sprite with rectangle image
CCSprite *sprite = [CCSprite spriteWithFile:#"Rectangle.png"];
When I touch the sprite, 8 red points will appear
Holding a point and drag it to scale(resize) the image like this
Can anyone show me how to do or give me a sample code.
I am not sure about this but you can redraw a texture on that 8 points.
It seems similar to draw texture of soft body. I have implemented that for my game and in that i use 12 points and on that 12 point i tried to draw the texture.
So what you can do is you have to redraw the texture on that points when any of the point dragged. This is a tutorial for soft body,but You can refer this tutorial for reference.
http://www.uchidacoonga.com/2012/04/soft-body-physics-with-box2d-and-cocos2d-part-44/
It's not the same thing what you are looking for but yes using this you can implement.
Related
I'm recently trying to build a program by myself, and for this reason I need to make a Sprite bigger when the MouseCursor is over it. Is solved it by checking for Mouse movement and then checking if ths mouse is over the Sprite. I increase the size of the sprite by using the setScale(); function. The problem then was, that the Sprite has been enlarged, but the center of the Sprite has been moved. I tried to set the Origin to the center of the Sprite, but now, the texture is at an other place than the Sprite.
The Texture is 500x500px large, so I set the origin to 250, 250. The Texture of the sprite moved to the right spot, since the origin is at an other place. The Position is "50, 250", so the texture is slightly off the screen. When I set a new position, the texture moves, but the sprite also. So texture and sprite don't match anymore.
Does anyone know how to fix the problem?
I want to create an app in Cocos2d/Cocos2dx in which i have an image which is not visible but when i move my finger on device it start drawing. Only that part of image draws where i move my finger.
Thanks in Advance
There are two ways I can think of drawing an image.
The first way would be like a brush. You would use RenderTexture and draw/visit a brush sprite to draw it into this texture. If you just need to draw with solid colors (can have opacity) you could also use the primitive draw commands (drawCircle, drawPoly, drawSegment). You will need a high rate of touch tracking and will likely want to draw segments or bezier curves between touch movements to catch fast movements.
http://discuss.cocos2d-x.org/t/using-rendertexture-to-render-one-sprite-multiple-times/16332/3
http://discuss.cocos2d-x.org/t/freehand-drawing-app-with-cocos2d-x-v3-3-using-rendertexture/17567/9
Searching on how other drawing games work would be useful.
The second way I can envision it is similar to revealing except using an inverse mask. So you would draw a set image, but reveal that image by drawing.
http://www.raywenderlich.com/4428/how-to-mask-a-sprite-with-cocos2d-2-0
There are more elaborate ways to handle the drawing into the RenderTexture in order to have the brush design tile correctly and repeat based on a given size, but that'll involve making something closer to an image editing tool.
I am developing a game in cocos2dx, I have a sprite of a maze, in png format (with walls and other areas are transparent).
I was wondering if there is any way I can detect collisions with these walls in the sprite. The player will drag an object through touch and solve the maze.
Any ideas are welcome.
basically what you need is map object's coordinates to image coordinates and then read pixel data at this image
for example, you have you sprite 8x8 pixels, but draw 256x256 world, ie each pixel in sprite is 32x32 texture
you have ball coordinates in "word", lets say 100x100, so just divide them by 8 = 12x12 - check this pixel in sprite
also, check out this tutorial for ios, it can give you additional ideas
I'm trying to show only a part of a background image (game scenenario in the future). The basic way to work is for example, first I draw a background image, after that i need to "hide"/cover the image with some dark or darness (no light, don't know what option must be chosen) and use the mouse click to using a circle or a triangle (my options) show only the part of the image background over with the circle/triangle centered on mouse position. I called this "lantern effect".
First Option: Play with the alpha channel, creating a an square covering all the window size and after that trying to substract the circle area over the alpha square over the image.
Second Option: Play again with a black square covering all the image background and trying to substract a circle/triangle. Try with glLogicOp but this method only plays mixing colors. Don't know how to do operation with 2D polygons with OpenGL.
...
Any other idea or easy example to learn how to do something similar.
Image example:
That's quite easy to achieve actually:
Create black texture with your lantern light shape in Alpha channel. (Texture could be animated)
Render background.
Render Lantern texture centered at your in-game mouse cursor.
Render black padding around the lantern texture to hide everything around till screen edges.
There's no need to use any special blending modes, just an overlay.
I was trying to use CCRenderTexture for pixel perfect collision detection, as outlined in this forum posting:
http://www.cocos2d-iphone.org/forum/topic/18522/page/2
The code "as is" works, and I have integrated it with my project
But I am having trouble doing some of the other things discussed:
If I create the renderTexture to be any size less than the screen size, the collision detection doesn't work properly - It seems to show collisions when the sprites are close (<15px) to each other but not actually colliding.
Also I have trouble changing the location of the render texture. Regardless of the position I specify, it seems to go from bottom left (0,0) till the width & height specified. I followed this post:
http://www.cocos2d-iphone.org/forum/topic/18796
But it doesn't solve my problem. I still get bad collisions like specified above. Also, the first post I mentioned on the list contains comments by many users who have resized their textures to 10x10, and repositioned them off screen.
Does anyone have any sample code, so I can see what I am doing wrong? I just use the boilerplate code:
CCRenderTexture* _rt = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];
_rt.position = CGPointMake(winSize.width*0.5f, winSize.height*0.5f);
[[RIGameScene sharedGameScene]addChild:_rt];
_rt.visible = YES;
I use cocos2d-iphone 1.0.1
You need to move the sprites you intend to draw into the region of the renderTexture before calling draw or visit. Moving the renderTexture does not change the position of _rt.sprite.
The intersection rectangle must be in the region of the renderTexture, otherwise you get inaccurate collisions.
It seems that you cannot change the position of _rt.sprite.
The solution that I use is to determine the origin (x,y) of the intersection box, and offset both the colliding sprites by that much. This will ensure that the intersection rectangle will have its origin at 0,0. Then I calculate the intersection rectangle again (after ensuring the origin of the intersection rect is 0,0) . Then I follow the instructions in the forum posting.
When determining the dimensions of the render texture, I ensure that they are at least as large as the intersection rectangle, and I ensure that the intersection rectangle is fully inside the render texture. This way there are accurate collisions. If even part of the intersection box is outside the render texture i get inaccurate collisions, so before drawing into the render texture, make sure you move the sprites you intend to visit so that the intersection box is entirely within the render texture.
Remember to move the sprites back after you're done. :)