CCLayerColor in SpriteKit - drawing

Is there something to replace CCLayerColor, CCLayerGradient etc inside Sprite Kit?
It was always easy to create simple filled rectangles in cocos2d, but I don't find any out of the box solutions in Sprite Kit so far.
As always, it should be platform independent so no iOS only code please (i.e., inside Sprite Kit).

Do you mean
SKSpriteNode *rect = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(70, 70)];

Related

Touch detection for SpriteBatchNode

I am curious if anyone knows how to detect when a SpriteBatchNode has been touched since it's BoundingBox is always null. This is how I detect touch for single sprites.
Node *parentNode = event->getCurrentTarget();
Vector<Node *> children = parentNode->getChildren();
Point touchPosition = parentNode->convertTouchToNodeSpace(touch);
for (auto iter = children.rbegin(); iter != children.rend(); ++iter) {
childObject = *iter;
if (childObject->getBoundingBox().containsPoint(touchPosition)){
//do something
}
But in most cases I want my sprites to be animated hence using SpriteBatchNode. Any ideas? Can I get the BoundingBox of the grandchildren since they are a series of sprites?
Depends on which method you are using.
Armature skeletal animation: Are you using the cocostudio skeletal animation tool to create your animations? If you use that you will get a node with the correct bounding box wrapping your sprite tightly and adjusting when bones change position
Sprite sheet animation: If you are using a sprite sheet with a .plist file you can inspect the size reflected in the .plist file and set your batchNode size to the biggest one you find, or dynamically adjust it based on the sprite currently being shown. I think cocos does this by default.
Loading sprite frames: If you are loading individual sprites using spriteFrames, you can inspect the contentSize of the spriteFrame and set your bounding box manually.
I have used all 3 and were always able to get the boundingBox size. Let me know if this helps. I used this article to learn sprite sheet animations and just played around with cocos skeletal animation and figured that out as I was experimenting with it.
Well I figured it out by getting the BoundingBox of the grandchild, which is a sprite. I was then able to do whatever I wanted to the spritebatchnode.

Scrolling region in cocos2d version 2

I am trying to implement a help screen in my cocos2d game, using cocos2d version 2.0. My screen will have a title bar ("Help") at the top and then the rest of the screen below that is where I want to put a scrolling help section. Ideally I would be able to put both text and images into this help window.
The problem is that cocos2d does not have any functionality like UIScrollView, and from what I have seen doing Google searches, every custom solution I have found seems to have problems with various bugs popping up on various devices.
I have tried these solutions thus far:
CCScrollLayer: http://www.cocos2d-iphone.org/forum/topic/17118/page/3
Scrolling CCNode: http://tonyngo.net/2011/11/scrolling-ccnode-in-cocos2d/
CCScrollView: http://bitbattalion.com/2011/09/uikit-uiscrollview-and-cocos2d/
The closest thing I got to work was embedding a UITextView but that seemed to randomly crash after a few scrolls so it seems unreliable to me.
Does anyone know of a good simple robust solution to this problem? It seems like it should be straightforward but it isn't.
I recommend that you make new class say:(HelpViewClass) and implement it with an UIScrollView and add whatever you want to add on UIScrollView and then you can use this as a child to your layer.
Steps
Make a class - inherited with UIView
Add UIScrollView to the View.
Add Your components to it.
Add this UIView to the HelpLayer.
You can add any UIKit component to the cocos2d Layer by using this
[[[CCDirector sharedDirector] view] addSubView:scrollView];
Note : Remove all UI component when you go back from this HelpLayer.
I think this may help you !

Cocos2d: Slowly reveal a sprite( think WoW spell cooldown )

I'm wanting to create a WoW cooldown effect where a player does some action and is not able to do the action again until the sprite is fully shown again. I have a grayed out version of the same sprite and am wanting to slowly reveal the sprite until it is fully available again. So, there will be a slow blend vertically of the gray and colored sprite.
Is there a way to do this with built in functionality with Cocos2d and CCSprite?
I'm using v2 of Cocos2d so I could write a shader which I think would be pretty easy, but before I went this route I wanted to see if there is an easier way.
Take a look to the CCProgressTimer class. If I understand right, it will make what you want
You can use CCFadeIn to animate the colored sprite over the grayed sprite :
[coloredSprite runAction:[CCFadeIn actionWithDuration:1.0f];

Qt GUI Development - Displaying a 2D grid using QGraphicsView

I'm new to Qt development so I've being trying to research a solution to a user interface I need to design. My project is to simulate players in an online game moving around a global map. To represent the map I need to display a 2D grid, with each space in the grid representing a region of a map. I then need to display the location of each player in the game. The back-end is all fully working, with the map implemented as a 2D array. I'm just stuck on how to display the grid.
The research I have done has led me to believe a QGraphicsView is the best way to do this, but I can't seem to find a tutorial relevant to what I need. If anyone has any tips on how to implement this it would be much appreciated.
Thanks, Dan
A 2D Grid is nothing more than a set of horizontal and vertical lines. Suppose you have a 500x500 map and you want to draw a grid where the distance between the lines in both directions is 50. The sample code that follows shows you how you can achieve it.
// create a scene and add it your view
QGraphicsScene* scene = new QGraphicsScene;
ui->view->setScene(scene);
// Add the vertical lines first, paint them red
for (int x=0; x<=500; x+=50)
scene->addLine(x,0,x,500, QPen(Qt::red));
// Now add the horizontal lines, paint them green
for (int y=0; y<=500; y+=50)
scene->addLine(0,y,500,y, QPen(Qt::green));
// Fit the view in the scene's bounding rect
ui->view->fitInView(scene->itemsVBoundingRect());
You should check the QGraphicsView and the QGraphicsScene documentation as well as the corresponding examples. Also you can watch the graphics view training videos or some graphics view related videos from the Qt developer days.
Well if you have a constant grid size or even a limited number of grid sizes what i like to do is to draw a grid block in gimp or any other program and then set that as the background brush (draw only bottom and right side of the block) qt will repeat the image and will give you a full grid. I think this is good for performance too.
This is the grid image i used in one of my programs it's 10x10 pixels.
Then call QGraphicsScene setBackgroundBrush as the follwing:
scene->setBackgroundBrush(QBrush(QPixmap(":/grid/grid10.png")));
The more native way is this:
scene = self.getScene() # Your scene.
brush = QBrush()
brush.setColor(QColor('#999'))
brush.setStyle(Qt.CrossPattern) # Grid pattern.
scene.setBackgroundBrush(brush)
borderColor = Qt.black
fillColor = QColor('#DDD')
rect = QRectF(0.0, 0.0, 1280, 720) # Screen res or whatever.
scene.addRect(rect,borderColor,fillColor) # Rectangle for color.
scene.addRect(rect,borderColor,brush) # Rectangle for grid.
Sorry by PyQt...
Suppose a scene is set to the graphicsview then simply below one line will show the grid.
ui->graphicsView->scene()->setBackgroundBrush(Qt::CrossPattern);
There several other values can be passed for ex: Qt::Dense7Pattern
These are members of enum BrushStyle, just click on any used value in Qt creator and it will take you to the enum declaration where you can see all other possible values.
PS:
A scene can be set like this:
ui->graphicsView->setScene(new QGraphicsScene());

cocos2d how CCSprite to appear on screen

I want to animate the way a CCSprite appear on screen.
At the moment I'm using "addChild" to display the sprite on screen but I want it to appear on screen with animation.
Is is possible?
Thanks
Try reading through the Cocos2D programming guide, there is a subject on animation with source code there. http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:index.