Scrolling Background - scroll

I'm trying out new things for a simple game in Portrait mode, I want to add a seamless scrolling background to a game, the image is 512x512px.
The Main scrolling I want to have is Vertically, but I want to be able to scroll sideways too within the bounds of the image size. How should I go about with this, I'm using cocos2d.
David

Try repeating the texture (in this case within 2000px)
CGRect repeatRect = CGRectMake(0, 0, 2000, 2000);
CCSprite *background = [CCSprite spriteWithFile:#"Background.png" rect:repeatRect];
ccTexParams params =
{
GL_NEAREST,
GL_NEAREST_MIPMAP_LINEAR,
GL_REPEAT,
GL_REPEAT
};
[background.texture setTexParameters:&params];

Related

Exclusion rects not shifting text properly with NSTextView/UITextView

I have an NSTextView with some exclusion rects so I can shift the text vertically without having to resort to adding newlines to the string. However, I've noticed that the exclusion rect is rather limited or perhaps even buggy because I cannot shift the text vertically more than ~45% of the textview's height. I have found a workaround where I increase the textview's height by 2x but this feels gross and I'd rather do it "properly"
In the image above, I have three text views (from left to right)
Programmatically without encapsulating it inside an NSScrollView, 2x height
Programmatically with NSScrollView encapsulation. 2x height
Using interface builder, regular height.
The exclusion rect is drawn with a CAShapeLayer and you can see that "hello world new world" isn't properly positioned outside of the exclusion rect.
I tried all three examples to make sure I wasn't missing something with regards to IB default settings or the dynamics of the text view when encapsulated in an NSScrollView (Im new to AppKit and TextKit), however all 3 examples exhibit the same bug.
Code to update the text exclusion rect
(Each time the slider moves on the bottom right, it will update their text rect)
label.stringValue = "excl: \(sender.integerValue): height: \(view.frame.height)"
print(sender.integerValue)
let exclHeight: CGFloat = CGFloat(slider.integerValue)
[aTextView, bTextView, cTextView]
.compactMap { $0 }
.forEach {
let rect = CGRect(
x: 5,
y: 5,
width: aTextView.frame.width - 10,
height: exclHeight)
$0.wantsLayer = true
$0.textContainer?.exclusionPaths.removeAll()
$0.textContainer?.exclusionPaths.append(.init(rect: rect))
$0.layer?.sublayers?.forEach {
$0.removeFromSuperlayer()
}
let layer = CAShapeLayer()
layer.frame = rect
layer.backgroundColor = NSColor.blue.withAlphaComponent(0.2).cgColor
layer.borderWidth = 1
layer.borderColor = NSColor.white.cgColor
$0.layer?.addSublayer(layer)
}
}
The problem seems to be that the exclusionPath is before the first line.
Just playing around with the parameters, a two line sample text with the rect y positioned after the first line works without any problems.
So it looks like the issue is calculating the container height when it starts with a exclusionPaths in -[NSLayoutManager usedRectForTextContainer:]
#interface LOLayoutManager : NSLayoutManager
#end
#implementation LOLayoutManager
- (NSRect)usedRectForTextContainer:(NSTextContainer *)container
{
NSRect rect = [super usedRectForTextContainer:container];
NSRect newRect = NSMakeRect(0, 0, NSMaxX(rect), NSMaxY(rect));
return newRect;
}
#end
Instead of returning y position from the exclusionPaths and the line fragments height, this returns a big rect starting at 0, 0. This should work as long as the NSTextView only contains one text container.

OpenGL: How to darken a sprite in Cocos2d

I've tried shooting the dark with all the GL functions, to no avail on what I'm sure has to be pretty simple. I'm new to it.
I want to take a sprite and simply darken it. My thoughts on this were to load the sprite, add a grey layer overtop of it (using CCLayerColor with a grey color), apply some kind of GL function, and then grab the output and display it onscreen. However, in every variant I tried where my source image was correctly darkened, the transparency around it was also affected, showing the grey. I need the darkening effect to be masked to the shape of the source sprite.
Here's the code I have so far. How can I correctly mask the darkening effect?
CCSprite* sprite = [CCSprite spriteWithSpriteFrameName: mod.backgroundImagePath];
CCLayerColor* tint = [CCLayerColor node];
[tint setColor:ccc3(205, 205, 205)];
//[tint setBlendFunc: (ccBlendFunc){GL_SRC_COLOR, GL_ONE_MINUS_DST_ALPHA}]; Need help here?
[sprite addChild:tint];
CCRenderTexture* rt = [CCRenderTexture renderTextureWithWidth:sprite.boundingBox.size.width height:sprite.boundingBox.size.height];
[rt begin];
[sprite visit];
[rt end];
You can just set the color property of CCSprite (which is inherited from CCNode):
CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:#"foobar"];
sprite.color = [CCColor colorWithRed:r green:g blue:b alpha:a];
Where the r,g,b,a variables are the color you want to use, normalized between 0 and 1.
I.e.:
Setting to 0,0,0,1 will make the object completely black.

Trying to mix UIKit Dynamics UICollision and core animation to scale a box's bounds

From a previous question I had.
CGAffineTransformScale does not send it's scale when using UIDynamicAnimator
Im trying to mix an animated scale effect on a box that has a smaller box on top of it.
Both boxes have UICollisionBehavior, I want to eventually make the bottom box thats getting larger scale fast enough to provide a velocity force I that will make the top box react with a bounce up.
The below is the scale code. The bounds is instantly set to its target final while the image in the view is still scaling. I want the bounds and image to be scaling at the same time so I can see the expected reactions happening.
- (IBAction)tapBoxThing:(UITapGestureRecognizer *)sender {
/* Make sure no translation is applied to this image view */
_boxView.transform = CGAffineTransformIdentity;
/* Begin the animation */
[UIView beginAnimations:nil
context:NULL];
/* Make the animation 5 seconds long */
[UIView setAnimationDuration:5.0f];
[_gravity removeItem:_boxView];
[_collision removeItem:_boxView];
// right here, the box will animate the image in the view to slowly fill in the new bounds.
// But I want the bounds to animate, its currently instantly scaling to teh final ammount.
// Core Animation states that frame is not animatable but bounds is.
CGRect frameZ = _boxView.bounds;
frameZ.size.height += 60.0f;
frameZ.size.width += 60.0f;
_boxView.bounds = frameZ;
[_gravity addItem:_boxView];
[_collision addItem:_boxView];
[UIView commitAnimations];
}

Resize all uiscrollviews in a container uiscrollview

Hierarchy is a fullscreen landscape container UIScroll. Inside are multiple fullscreen pages made up of uiscrollviews with images inside - this allows paging plus pinching /zooming of the inner scrollviews. You can scroll up and down.
I want to resize the width of the container scrollview and have the nested uiscrolls and images within them resize proportionally and dynamically to the new width and height.
My outer scroll has autoresize to yes, my inner scrolls have the same. And I also have my uiimageviews on auto as well.
Right now the frame of the container scroll just crops off the inner content.
Any thoughts?
I ended up adding code to a function that allowed me to scale the interior of the scrollview the way I wanted.
[containerscrollView setFrame:CGRectMake(200.0f, 0.0f, 800.0f, 768.0f)];
containerscrollView.contentSize = CGSizeMake(800,containerscrollView.frame.size.height * scrollArrayCount);
for (UIScrollView *tmp in [containerscrollView subviews]) {
if (tmp.tag>4000) // make sure to only resize the inner scrollview {
[tmp setFrame:CGRectMake(tmp.frame.origin.x, tmp.frame.origin.y, 800.0f, 768.0f)];
_innerScrollView.contentSize =CGSizeMake(800.0f, 768.0f);
}
}

SCNView Re-sizing Issue

I am trying to render 3D bar chart in SCNView using ScreenKit framework.
My rendering code is,
int height=10,y=0,x=0;
for (int i=0; i<10; i++) {
SCNBox *box1 = [SCNBox boxWithWidth:4 height:height length:2 chamferRadius:0];
boxNode1 = [SCNNode nodeWithGeometry:box1];
boxNode1.position = SCNVector3Make(x, y, 0);
SCNMaterial *material = [SCNMaterial material];
material.diffuse.contents = (NSColor *)[self.colorArray objectAtIndex:i%6];
material.specular.contents = [NSColor whiteColor];
material.shininess = 1.0;
box1.materials = #[material];
//boxNode1.transform = rot;
[scene.rootNode addChildNode:boxNode1];
x+=6;
height+=10;
y += 5 ;
}
I can render but while re-sizing the view the chart bars goes to the center of the view.
I need to render the chart, which cover the margins of the view and when Re-size it have to change accordingly. The image(s) below shows my problem.
Original Image:
Image where less stretching of both windows:
Can anyone please help me to fix the issue.
The the windows in the image that you had linked to in your original question was very stretched and that made it very hard to see what was going on. When I took that image and made the windows less stretched it was easier to have some idea of what is going on.
I think that you are seeing a general resizing issue. Either you are using springs and struts and have configured flexible margins on the left and right or you are using auto layout with a centered view with fixed width.
I assume that the red boxes that I have drawn in the image below is the bounds of your scene view in both these cases. You can easily see if this is the case by giving the scene view a different background color and resize it again.
My solution to your problem would be to change how your view resizes as the window resizes, to better meet your expectations.

Resources