Quick Code Samples
From Pixelwave
Here are a few code snippets to show you just how simple it is to work with Pixelwave.
Contents |
1 Working with Images
Loading an Image
PXTexture *texture = [PXTexture textureWithContentsOfFile:@"image.jpg"];
Displaying a texture on the screen
[self addChild:texture];
Rendering to texture in real-time
PXTextureData *textureData = [[PXTextureData alloc] initWithWidth:512 andHeight:512]; [textureData drawDisplayObject:self.stage];
2 Simple transformations
Setting the position of a graphic
texture.x = 100; texture.y = 150;
Setting the size of a graphic;
// Set the size to 60% texture.scale = 0.6;
// Set the width to 60% texture.scaleX = 0.6;
// Set the width to 100 px texture.width = 100;
Setting the opacity of a graphic
// Set opacity to 50% texture.alpha = 0.5f;
Animated rotation
[self addEventListenerOfType:PXEvent_EnterFrame listener:PXListener(onFrame:)];
// ... later that cycle in the functions section:
- (void) onFrame:(PXEvent *)event
{
texture.rotation += 10.0f;
}
3 Graphic containers
Adding a graphic to a container
[container addChild:texture];
Removing a graphic from a container
[container removeChild:texture];
Referencing children by name
PXDisplayObject *child = [container childByName:@"button"];
Clearing the contents of a container
[container removeAllChildren];
4 Advanced transformations
Manipulating the transformation matrix of a display object
PXMatrix *matrix = texture.transform.matrix; [matrix rotate:M_PI/6.0f]; //30 degrees [matrix scaleX:0.5f andY:2.0f]; [matrix translateX:50.0f andY:75.5f]; texture.transform.matrix = matrix;
Or to do the same thing you can edit the matrix elements directly like so:
PXMatrix *matrix = texture.transform.matrix; matrix.a = 0.433013f; matrix.b = 0.5f; matrix.c = -0.5f; matrix.d = 1.732051f; matrix.tx = 50.0f; matrix.ty = 75.5f; texture.transform.matrix = matrix;
5 Drawing vector shapes
drawing a vector box
PXShape *shape = [PXShape new]; [shape.graphics lineStyleWithThickness:1.0f color:0xff7777 alpha:1.0f]; [shape.graphics drawRectWithX:20.0f andY:72.0f andWidth:300.0f andHeight:108.0f];
drawing a filled vector box
[shape.graphics beginFill:0x5fa7c9 alpha:1.0f]; [shape.graphics drawRectWithX:20.0f andY:72.0f andWidth:300.0f andHeight:108.0f]; [shape.graphics endFill];
6 Working with text
A simple label
PXTextField *textField = [PXTextField new]; textField.text = @"It's 10 O'clock, do you know where your pants are?"; [self addChild:textField];
Changing fonts
textField.font = @"Zapfino";
Creating a texture font (for super fast rendering) with a system font
[PXTextureFont registerSystemFontWithFont:@"Tahoma" name:@"TahomaTextureFont" size:30.0f characterSets:PXFontCharacterSet_AllLetters specialCharacters:@",!"]; textField.font = @"TahomaTextureFont";
Creating a texture font (for super fast rendering) with an external font
[PXTextureFont registerTextureFontWithContentsOfFile:@"font.ttf" name:@"customFont" size:16.0f characterSets:PXFontCharacterSet_AllLetters specialCharacters:@",!"]; textField.font = @"customFont";
7 Working with audio
Loading a sound
PXSound *sound = [PXSound soundWithContentsOfFile:@"sound.wav"];
Playing a sound
[sound play];
Playing a sound with loops
PXSound *sound = [PXSound soundWithContentsOfFile:@"suctionCupOff.wav"]; // Loop 3 times [sound playWithStartTime:0 loopCount:3 soundTransform:nil];
Playing a sound with infinite loops, starting at a mid-point
// Start 4.5 seconds in and play forever [sound playWithStartTime:4500 loopCount:PX_SOUND_INFINITE_LOOPS soundTransform:nil];
Playing a sound in 3D
PXSoundTransform3D *soundTransform3D = [PXSoundTransform3D new]; soundTransform3D.x = 40.0f; soundTransform3D.y = 15.0f; soundTransform3D.z = -21.0f; [sound playWithStartTime:0 loopCount:0 soundTransform:soundTransform3D];
Changing the volume/pitch of a sound
PXSoundChannel *channel = [sound play]; PXSoundTransform *transform = channel.soundTransform; // Set to 50% gain transform.volume = 0.5; // Double the pitch transform.pitch = 2.0; channel.soundTransform = transform;
8 Touch Interaction
Listening to touch events
[self addEventListenerOfType:PXTouchEvent_TouchDown listener:PXListener(onTouchDown:)];
// ... later that cycle in the functions section:
- (void) onTouchDown:(PXTouchEvent *)event
{
NSLog(@"Touch occurred");
}
Following a moving touch with a graphic
[self addEventListenerOfType:PXTouchEvent_TouchMove listener:PXListener(onTouchDown:)];
// ...
- (void) onTouchMove:(PXTouchEvent *)event
{
texture.x = event.localX;
texture.y = event.localY;
}
9 General tasks
Setting the framerate
self.stage.frameRate = 60.0f;
Setting the background color of the stage
self.stage.backgroundColor = 0x82A200;
Grabbing a random number
float randomVal = [PXMath random];
Grabbing a random number in range
float randomFloat = [PXMath randomFloatInRangeFrom:7.0 to 27.0];
int randomInt = [PXMath randomIntInRangeFrom:-10 to 10];
Getting the time since the app started
// Returns the time in seconds since the Pixelwave framework was initialized double secondsSinceStart = PXGetTimerSec(); // Returns the time in milliseconds since the Pixelwave framework was initialized. long milliscondsSinceStart = PXGetTimer();
Turning on bounding box rendering for debugging
PXDebug.drawBoundingBoxes = YES;
- This page was last modified on 12 August 2011, at 05:44.