Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I am trying to integrate a slightly modified version of Boids flocking Sim into my React website. I have tried with both wrapper and p5-React npm's, both work on a smaller scale (i.e. with the examples in the descriptions), but when I try to apply it to the boids sim it just shows "loading...". I have watched the youtube tutorial and fully understand the logic, but I am a bit new to p5. Here is what I am trying to include: https://codepen.io/stephenleemorrow/pen/QWaxKqY
Here is my VS Code:
let flock;
let img;
let bg;
p5.preload = () => {
img = p5.loadImage("src\pngbyte.com-Open-Window-Window-Png-Image-window-png-images-open-bar.png");
}
p5.setup = () => {
bg = p5.loadImage("src\pixle-cloud-landscape.webp");
p5.createCanvas(568, 320);
flock = new Flock();
// Add an initial set of boids into the system
for (let i = 0; i < 20; i++) {
let b = new Boid(p5.width / 2, p5.height / 2);
flock.addBoid(b);
}
}
p5.draw = (p5) => {
p5.background(51);
flock.run();
p5.image(img, -23, -10, 610, 340);
}
// Add a new boid into the System
p5.mouseDragged = () => {
flock.addBoid(new Boid(p5.mouseX, p5.mouseY));
}
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Flock object
// Does very little, simply manages the array of all the boids
function Flock() {
// An array for all the boids
this.boids = []; // Initialize the array
}
Flock.prototype.run = function () {
for (let i = 0; i < this.boids.length; i++) {
this.boids[i].run(this.boids); // Passing the entire list of boids to each boid individually
}
}
Flock.prototype.addBoid = function (b) {
this.boids.push(b);
}
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Boid class
// Methods for Separation, Cohesion, Alignment added
function Boid(x, y) {
this.acceleration = p5.createVector(0, 0);
this.velocity = p5.createVector(p5.random(-1, 1), p5.random(-1, 1));
this.position = p5.createVector(x, y);
this.r = 3.0;
this.maxspeed = 3; // Maximum speed
this.maxforce = 0.05; // Maximum steering force
}
Boid.prototype.run = function (boids) {
this.flock(boids);
this.update();
this.borders();
this.render();
}
Boid.prototype.applyForce = function (force) {
// We could add mass here if we want A = F / M
this.acceleration.add(force);
}
// We accumulate a new acceleration each time based on three rules
Boid.prototype.flock = function (boids) {
let sep = this.separate(boids); // Separation
let ali = this.align(boids); // Alignment
let coh = this.cohesion(boids); // Cohesion
// Arbitrarily weight these forces
sep.mult(1.5);
ali.mult(1.0);
coh.mult(1.0);
// Add the force vectors to acceleration
this.applyForce(sep);
this.applyForce(ali);
this.applyForce(coh);
}
// Method to update location
Boid.prototype.update = function () {
// Update velocity
this.velocity.add(this.acceleration);
// Limit speed
this.velocity.limit(this.maxspeed);
this.position.add(this.velocity);
// Reset accelertion to 0 each cycle
this.acceleration.mult(0);
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
Boid.prototype.seek = function (target) {
let desired = p5.Vector.sub(target, this.position); // A vector pointing from the location to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(this.maxspeed);
// Steering = Desired minus Velocity
let steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxforce); // Limit to maximum steering force
return steer;
}
Boid.prototype.render = function () {
// Draw a triangle rotated in the direction of velocity
let theta = this.velocity.heading() + p5.radians(90);
p5.fill(50);
p5.stroke(100);
p5.push();
p5.translate(this.position.x, this.position.y);
p5.rotate(theta);
p5.beginShape();
p5.vertex(0, -this.r * .5);
p5.vertex(-this.r, this.r * .5);
p5.vertex(this.r, this.r * .5);
p5.endShape(p5.CLOSE);
p5.pop();
}
// Wraparound
Boid.prototype.borders = function () {
if (this.position.x < -this.r) this.position.x = p5.width + this.r;
if (this.position.y < -this.r) this.position.y = p5.height + this.r;
if (this.position.x > p5.width + this.r) this.position.x = -this.r;
if (this.position.y > p5.height + this.r) this.position.y = -this.r;
}
// Separation
// Method checks for nearby boids and steers away
Boid.prototype.separate = function (boids, p5) {
let desiredseparation = 25.0;
let steer = p5.createVector(0, 0);
let count = 0;
// For every boid in the system, check if it's too close
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < desiredseparation)) {
// Calculate vector pointing away from neighbor
let diff = p5.Vector.sub(this.position, boids[i].position);
diff.normalize();
diff.div(d); // Weight by distance
steer.add(diff);
count++; // Keep track of how many
}
}
// Average -- divide by how many
if (count > 0) {
steer.div(count);
}
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(this.maxspeed);
steer.sub(this.velocity);
steer.limit(this.maxforce);
}
return steer;
}
// Alignment
// For every nearby boid in the system, calculate the average velocity
Boid.prototype.align = function (boids) {
let neighbordist = 50;
let sum = p5.createVector(0, 0);
let count = 0;
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
if ((d > 0) && (d < neighbordist)) {
sum.add(boids[i].velocity);
count++;
}
}
if (count > 0) {
sum.div(count);
sum.normalize();
sum.mult(this.maxspeed);
let steer = p5.Vector.sub(sum, this.velocity);
steer.limit(this.maxforce);
return steer;
} else {
return p5.createVector(0, 0);
}
}
// Cohesion
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
Boid.prototype.cohesion = function (boids) {
let neighbordist = 50;
let sum = p5.createVector(0, 0); // Start with empty vector to accumulate all locations
let count = 0;
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
if ((d > 0) && (d < neighbordist)) {
sum.add(boids[i].position); // Add location
count++;
}
}
if (count > 0) {
sum.div(count);
return this.seek(sum); // Steer towards the location
} else {
return p5.createVector(0, 0);
}
}
}
Again, the connection to App() is good cause it works with smaller sketches. I think I'm just missing something in the syntax.
Any help would be GREATLY appreciated!
I made a few changes and got this running (although with errors in the p5 code that kind of fall out of the scope of the answer).
This might be a typo, but to use this sketch with ReactP5Wrapper, you need to export it as a function, so your code above is missing
export const sketch = (p5) => {
at the start. You actually have the closing bracket at the end, so it's probably a typo in the question.
Later in the code, anywhere you are passing p5 into a function, like
Boid.prototype.separate = function (boids, p5) {
you are not passing anything in for p5 - like
let sep = this.separate(boids);
so that would cause undefined errors. The p5 passed into the sketch function can be used throughout the sketch so you can remove p5 from the parameter lists of these other functions.
The really breaking issue here though was that you were trying to load images from the /src/ folder.
Make a directory in the /public/ folder at the top level of your project, call it /assets/ and put your images in there.
Then you can reference them like:
p5.preload = () => {
img = p5.loadImage("assets/test.png");
}
After I do all that, I get the sketch starting, creating the canvas and doing some of the algorithm but it fails at various points that I'm sure will have loads of fun debugging after you get it running to begin with :)
Btw, I'm displaying the sketch in my App component like this, using the ReactP5Wrapper:
import React from 'react';
import { ReactP5Wrapper } from 'react-p5-wrapper';
import { sketch } from './sketch/sketch';
export const App = () => {
return(
<div>
<ReactP5Wrapper sketch={sketch}/>
</div>
)
}
Related
I use a function (shown below) to spawn a coin node in specific locations at random using an array.
Using this function, I am trying to incorporate more than one coin node (that are slightly different from one another) into this function so that multiple nodes can use this array to spawn and function just like the first coin node.
The problem that I have is that when I incorporate another node into this function or make a new but similar function for the 2nd node I get a Thread 1 SIGABERT error after the game crashes.
I currently have two separate functions for each node that are very similar, but with slight differences to accommodate each node.
func generateCoinZero() {
if(self.actionForKey("spawningCoinZero") != nil){return}
let coinTimerZero = SKAction.waitForDuration(2, withRange: 7)
let spawnCoinZero = SKAction.runBlock {
let coinZeroTexture = SKTexture(imageNamed: "coinZero")
self.coinZero = SKSpriteNode(texture: coinZeroTexture)
self.coinZero.physicsBody = SKPhysicsBody(circleOfRadius: self.coinZero.size.height / 12)
self.coinZero.physicsBody?.dynamic = false
self.coinZero.physicsBody?.allowsRotation = false
self.coinZero.zPosition = 1
self.coinZero.physicsBody?.categoryBitMask = ColliderType.coinZeroCategory
self.coinZero.physicsBody?.contactTestBitMask = ColliderType.playerCategory
self.coinZero.physicsBody?.collisionBitMask = 0
self.player.physicsBody?.categoryBitMask = ColliderType.playerCategory
self.player.physicsBody?.contactTestBitMask = 0
self.player.physicsBody?.collisionBitMask = ColliderType.boundary
var coinPositionZero = Array<CGPoint>()
coinPositionZero.append((CGPoint(x:250, y:139)))
coinPositionZero.append((CGPoint(x:790, y:298)))
coinPositionZero.append((CGPoint(x:225, y:208)))
coinPositionZero.append((CGPoint(x:220, y:237)))
let spawnLocationZero = coinPositionZero[Int(arc4random_uniform(UInt32(coinPositionZero.count)))]
let actionZero = SKAction.repeatActionForever(SKAction.moveToX(+self.xScale, duration: 2.0))
self.coinZero.runAction(actionZero)
self.coinZero.position = spawnLocationZero
self.addChild(self.coinZero)
print(spawnLocationZero)
}
let sequenceZero = SKAction.sequence([coinTimerZero, spawnCoinZero])
self.runAction(SKAction.repeatActionForever(sequenceZero), withKey: "spawningCoinZero")
}
func generateCoinTwo() {
if(self.actionForKey("spawnCoinTwo") != nil){return}
let coinTimerTwo = SKAction.waitForDuration(2, withRange: 7)
let spawnCoinTwo = SKAction.runBlock {
let coinTwoTexture = SKTexture(imageNamed: "coinTwo")
self.coinTwo = SKSpriteNode(texture: coinTwoTexture)
self.coinTwo.physicsBody = SKPhysicsBody(circleOfRadius: self.coinTwo.size.height / 12)
self.coinTwo.physicsBody?.dynamic = false
self.coinTwo.physicsBody?.allowsRotation = false
self.coinTwo.zPosition = 1
self.addChild(self.coinTwo)
var coinPositionTwo = Array<CGPoint>()
coinPositionTwo.append((CGPoint(x:250, y:139)))
coinPositionTwo.append((CGPoint(x:790, y:298)))
coinPositionTwo.append((CGPoint(x:225, y:208)))
coinPositionTwo.append((CGPoint(x:220, y:237)))
let spawnLocationTwo = coinPositionTwo[Int(arc4random_uniform(UInt32(coinPositionTwo.count)))]
let actionTwo = SKAction.repeatActionForever(SKAction.moveToX(+self.xScale, duration: 2.0))
self.coinTwo.runAction(actionTwo)
self.coinTwo.position = spawnLocationTwo
self.addChild(self.coinTwo)
print(spawnLocationTwo)
}
let sequenceTwo = SKAction.sequence([coinTimerTwo, spawnCoinTwo])
self.runAction(SKAction.repeatActionForever(sequenceTwo), withKey: "spawnCoinTwo")
}
OK, there are quite a lot of issues here, the main ones being the extreme duplication of code and having your generateCoin...-functions doing way too much. So here goes:
You state in the comments that the scene should have two coins spawning at different times at one of four possible positions. If the scene has two coins, then the scene has two coins. Let's just create these as properties and be done with it:
// Your two coin properties
let coin1 = coinNode()
let coin2 = coinNode()
// the function from which they are created
func coinNode() -> SKSpriteNode {
let coinNode = SKSpriteNode(imageNamed: "coinZero")
coinNode.physicsBody = SKPhysicsBody(circleOfRadius: coinNode.size.height / 2)
coinNode.physicsBody?.dynamic = false
coinNode.physicsBody?.allowsRotation = false
coinNode.zPosition = 1
coinNode.physicsBody?.categoryBitMask = ColliderType.coinZeroCategory
coinNode.physicsBody?.contactTestBitMask = ColliderType.playerCategory
coinNode.physicsBody?.collisionBitMask = 0 // A ColliderType.none would be lovely...
return coinNode
}
Now, these coins are not yet added to the scene nor do they have a proper position, this sounds like a fitting scope for another function:
func addCoin() {
let positions = [ CGPoint(x:250, y:139), CGPoint(x:790, y:298), CGPoint(x:225, y:208), CGPoint(x:220, y:237)]
let position = positions[Int(arc4random_uniform(UInt32(positions.count)))]
if coin1.parent == nil {
coin1.position = position
addChild(coin1)
} else if coin2.parent == nil {
coin2.position = position
addChild(coin2)
}
}
Finally you want to have this function being called so do the following in your scene's init or setup:
let delay = SKAction.waitForDuration(1) // or however long you want it to be between each spawn
let addCoinCall = SKAction.runBlock({ self.addCoin() })
let spawnSequence = SKAction.sequence([delay, addCoinCall])
runAction(SKAction.repeatActionForever(spawnSequence))
You can't addChild twice, put addChild out of runBlock, and make sure that you are addChild once.
If you want put multiple coin, is better to copy your node and add on the scene.
You can create a node like coinZero out of function, and then inside the function do something like:
let coinToAdd = coinZero.copy()
I'm having this issue with this memory match game. I want the cards to show face up so the user can memorize them, and after 3 seconds I want them to turn over and show their back face.
I got the entire game working perfectly, btw. I'm just missing this detail!
the code below is how my cards get created (they are all 1 movieclip (mcCartas) with 37 frames inside (36 types of card and back card))
const QUANT_CARTAS:int = 36; ///// number of cards (faces drawn, frame numbers)
const CARTAS_POR_LINHA:int = 6; /// number of lines (6x6 grid)
var cartas:Array = new Array(); // cards array
var cartasColetadas:Array = new Array(); // clicked cards array
////////gets the 36 cards into the array
for(var i:int=0;i<QUANT_CARTAS;i++)
{
cartas.push(i);
}
/////////shuffles the cards
for(var moeda:int = QUANT_CARTAS-1;moeda>0;moeda--)
{
var pos:int = Math.floor(Math.random() * moeda);
var carta:int = cartas[moeda];
cartas[moeda] = cartas[pos];
cartas[pos] = carta;
}
////////// puts them on the table
for(i=0;i<QUANT_CARTAS;i++)
{
var novaCarta:Carta = new Carta();
novaCarta.tipoCarta = cartas[i];
novaCarta.x = 5 + (novaCarta.width + 2.7) * (i % CARTAS_POR_LINHA);
novaCarta.y = 5 + (novaCarta.height + 2.7) * (Math.floor(i/CARTAS_POR_LINHA));
novaCarta.gotoAndStop(cartas[i]+1); // this line they all face their number OR
//novaCarta.gotoAndStop(QUANT_CARTAS + 1); // this line they all face back (last frame)
novaCarta.buttonMode = true;
novaCarta.addEventListener(MouseEvent.CLICK, cartaClicada);
addChild(novaCarta);
trace (cartas);
if(i == 35)
{
podeJogar = false;
mcContagem.x = 884;
mcContagem.y = 511;
addChild(mcContagem);
intervalo = setInterval(desviraCartas, 3000);
function desviraCartas()
{
for(var j:int = 0;j < QUANT_CARTAS; j++)
{
//here I'm trying to make them go back to their back (last frame)
//something????.gotoAndStop(QUANT_CARTAS + 1);
if(j == 35)
{
clearInterval(intervalo);
iniciaJogo();
}
}
}
}
}
this code IS working, but a couple lines up where I typed "something????.gotoAndStop etc" ... that's where I have tried everything... heeelppppp plzzzzz
You should store the new Cartas in an array when you add them. Then use that array to reference them: Push into array:
aCartasMCs.push(novaCarta);
Then you can reference like:
aCartasMCs[i].gotoAndStop(QUANT_CARTAS + 1);
//I commented out the section that I was trying, but so far nothing has been working. I know I need to set up a loop or something of that kind to get rid of the items in the array, but I'm not sure how to get the right outcome.
THANKS SO MUCH
import flash.utils.Timer;
import flash.display.MovieClip;
import flash.events.Event;
var loveXCounter: Number = 0;
healthLove_txt.text = "Wrong Words: 0";
var insideLoveXCount = new Timer(4000, 0)
insideLoveXCount.addEventListener(TimerEvent.TIMER, countLoveX);
insideLoveXCount.start();
var loveXList: Array = ["Hate", "Abhor", "Vile", "Dislike", "Enmity", "Illwill", "Loath", "Neglect", "Repulse", "Resent"];
function countLoveX(e: TimerEvent) {
var loveXName: String = loveXList[Math.floor(Math.random() * loveXList.length)];
var loveXReference: Class = getDefinitionByName(loveXName) as Class;
var myLoveX = new loveXReference;
myLoveX.x = Math.random() * 700;
myLoveX.y = -10;
myLoveX.speed = 5;
myLoveX.addEventListener(Event.ENTER_FRAME, fallingLoveX);
addChild(myLoveX);
loveXList.splice(loveXName, 1);
}
function fallingLoveX(e: Event): void {
if (e.target.hitTestObject(fb_mc)) {
e.target.visible = false;
loveXCounter = loveXCounter + 1;
e.target.removeEventListener(Event.ENTER_FRAME, fallingLoveX);
healthLove_txt.text = "Wrong Words: " + loveXCounter;
if (loveXCounter == 5) {
while( loveXList.length > 0 )
{
this.removeChild( loveXList[loveXList.length - 1] );
loveXList.pop();
}
MovieClip(root).gotoAndStop(137);
loseBook_mc.gotoAndStop("loseLove");
}
}
if (e.target.y < 800) {
e.target.y += e.target.speed;
if (e.target.y > 800) {
e.target.y = 800;
}
} else {
e.target.visible = false;
}
}
stop();
I can see that the line...
loveXList.splice(loveXName, 1);
...might be causing problems. Firstly, the first parameter of the Array .splice method should be of type int. You are passing in a String. To remedy that, store the randomly selected index of the loveXList Array in an int variable, use that get your loveXName String and use the int again to splice out the correct String from the loveXList Array, ie:
var randomIndex:int = Math.floor(Math.random() * loveXList.length);
var loveXName: String = loveXList[randomIndex];
...
loveXList.splice(randomIndex, 1);
Secondly, you will quickly run out of Strings to splice out of the loveXList Array so, at the start of the countLoveX function, check if loveXList.length>0. If loveXList.length==0 you may want to stop the timer.
I want to make a program in which mines will appear on the screen after a user inputs the number of mines. Then, the user will click on one mine and set off a chain reaction that explodes the nearest two mines.
So far, my code can prompt the user for the number of mines, and then display them. The mines are buttons in which, when clicked, will be removed and an explosion will appear.
However, I am stuck with how I can handle the chain reaction. I am relatively new to coding in AS3 and therefore am stumped with no clue on how to approach this part of my program.
Code:
package
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.events.*;
public class Minefield extends MovieClip
{
var integer:int;
var iField:TextField = new TextField();
var button:iButton = new iButton();
var i:int;
var mines:Array = new Array();
public function Minefield()
{
var explosion:iExplosion = new iExplosion();
iField.type = "input";
iField.height = 18;
iField.x = 460;
iField.y = 275;
iField.border = true;
iField.restrict = "0-9";
iField.maxChars = 2;
stage.focus = iField;
addChild(iField);
addChild(button);
button.x = 450;
button.y = 175;
button.buttonMode = true;
button.addEventListener(MouseEvent.CLICK, UponClick);
}
function AddMines()
{
for (i = 0; i < integer; i++)
{
CreatorOfMine();
mines[i].addEventListener(MouseEvent.CLICK, UponMineClick)
mines[i].buttonMode = true;
}
}
function CreatorOfMine()
{
mines[i] = new Mine();
MineLocation()
}
function MineLocation()
{
mines[i].x = Math.round(Math.random() * 925);
mines[i].y = Math.round(Math.random() * 525);
mines[i].rotation = Math.random() * 360;
addChild(mines[i]);
}
function UponClick(e:MouseEvent)
{
integer = int(iField.text);
RemoverOfChildren();
}
function RemoverOfChildren()
{
removeChild(button);
removeChild(iField);
AddMines();
}
function UponMineClick(event:MouseEvent){
var mineObject:Mine = Mine(event.currentTarget)
var expl:iExplosion = new iExplosion()
expl.x = mineObject.x
expl.y = mineObject.y
expl.rotation = mineObject.rotation
addChild(expl)
removeChild(mineObject)
}
}
}
}
Information you may need/want:
Stage size is 1024 x 600 (px)
Size of mine(s) is 40 x 40 (px)
Size of explosion is 40 x 40 (px)
Looks like a good case for recursion. Pseudo-code:
Function ExplodeMine(mine) {
mine.boooooooooom()
nearest = findNearestUnexplodedMines()
foreach(nextMine in nearest) {
ExplodMine(nextMine);
}
}
Just start ExplodeMine on the first mine that is clicked.
I'm trying to build a generic slide show flash file controlled with key presses... right and left arrow to go to next / previous slide, down and up to call / uncall the single animations / bullet points / whatever on a single slide.
So far I managed to get a file that can load all the pages from a movieclip and flick through them with right and left arrow and a nice fade out and fade in effect, even when flicking very fast.
What I do not get at all after several hours and lots of google searches is how to now control the single page movieclips. I uploaded the file so far:
http://www.broesel-brzelius.de/zeug/slide.zip
You can see that my (as of yet crude) attempt to control the pageHolder.Pages.page1 movieclip to gotoAndStop(2) just results in the pageHolder.Pages movieclip that was loaded to position 0 in the array to go to frame 2 and thus display the second page.
Code so far:
import flash.display.Sprite;
import flash.events.MouseEvent;
import com.greensock.*;
import flash.display.MovieClip;
// instantiate a variable to find number of pages
var numberOfPages:Pages = new Pages();
// instantiate an array to hold the page movieclips
var pageArray:Array = new Array();
// instantiate a container to hold the pages
var pageHolder:Sprite = new Sprite();
// declare variables that will hold reference to the current page IDs
var targetIDold:int = 0;
var targetIDnew:int = 0;
// declare a variable that will hold the current direction of slide movement
var movement:int = -1;
// call a function that builds the application
// pass in the number of pages in fl_prevSlide
buildApp(numberOfPages.totalFrames);
// this function builds the application
function buildApp(n:int):void
{
// declare variables for the pages
var p:Pages;
// instantiate a new Page, send its playhead to the current value of i+1
// it is necessary to add 1 to the value of i, since i starts at 0, while the timeline starts at 1
for (var i:int = 0; i < n; i++)
{
p = new Pages();
p.gotoAndStop(i + 1);
pageArray.push(p);
}
// set the position of the pageHolder relative to the buttonHolder
pageHolder.x = pageHolder.y = 0;
// add the first page (at index 0) from pageArray to pageHolder
pageHolder.addChild(pageArray[targetIDnew]);
// add pageHolder and buttonHolder to the stage
addChild(pageHolder);
}
//Adding Listener and corresponding function to change between slides
stage.addEventListener(KeyboardEvent.KEY_DOWN, f_changeSlide);
function f_changeSlide(evt:KeyboardEvent):void
{
if(evt.keyCode == 37) // left arrow
{
f_prevSlide();
}
else if (evt.keyCode == 39 || evt.keyCode == 32) // right arrow or space
{
f_nextSlide();
}
}
function f_prevSlide():void
{
if(targetIDnew > 0)
{
movement = -1;
targetIDnew -= 1;
f_addPage();
}
}
function f_nextSlide():void
{
if(targetIDnew < (numberOfPages.totalFrames - 1))
{
movement = 1;
targetIDnew += 1;
f_addPage();
}
}
function f_addPage():void
{
// use the targetID variable to access the corresponding index in the pageArray and assign it to a temporary variable
targetIDold = targetIDnew - movement;
var _mcOld:MovieClip = MovieClip(pageArray[targetIDold]);
var _mcNew:MovieClip = MovieClip(pageArray[targetIDnew]);
//avoid flickering
if (_mcNew.alpha == 1) {
_mcNew.alpha = 0;
}
// add the temp variable to pageHolder
pageHolder.addChild(_mcNew);
// tween the temp variable to the specified properties, then call a function to remove the previous page
// new page gets faded in, old page gets faded out
TweenMax.to(_mcNew, 1.5, {alpha:1});
TweenMax.to(_mcOld, 1.5, {alpha:0, onComplete:f_removePage});
}
function f_removePage():void
{
// previous page will always be at index 0; remove it
// the new page just added will drop down to index 0 when the previous page is removed
pageHolder.removeChildAt(0);
}
//Trying to advance / decrease the timeline in the movieclips of the single pages by pressing down / up
stage.addEventListener(KeyboardEvent.KEY_DOWN, f_changeFrame);
function f_changeFrame(evt:KeyboardEvent):void
{
if(evt.keyCode == 40) // down arrow
{
f_nextFrame();
}
else if (evt.keyCode == 38) // up arrow
{
f_prevFrame();
}
}
function f_nextFrame():void
{
var w:uint = pageHolder.numChildren - 1 ;
(pageHolder.getChildAt(w) as MovieClip).gotoAndStop(currentFrame + 1);
// for (var i:uint = 0; i < pageHolder.numChildren; i++)
// {
// trace (+i+'.\t name:' + pageHolder.getChildAt(i).name + '\t type:' + typeof (pageHolder.getChildAt(i)));
// }
trace ("down!");
}
function f_prevFrame():void
{
var w:uint = pageHolder.numChildren - 1 ;
(pageHolder.getChildAt(w) as MovieClip).gotoAndStop(currentFrame - 1);
// for (var i:uint = 0; i < pageHolder.numChildren; i++)
// {
// trace (+i+'.\t name:' + pageHolder.getChildAt(i).name + '\t type:' + typeof (pageHolder.getChildAt(i)));
// }
trace ("up!");
}
Edit: clarified code a bit
Edit2: TL;DR: Problem is the last bit of code with the three functions that fail to control the timeline of mc page1, which is a child of mc pages, which is pushed frame by frame into an array and loaded at runtime into the sprite pageHolder. How to control the timeline of the single page movieclips?
Finally got it to work. This will work as a perfect powerpoint replacement :D
You will need to name the single slides (one movieclip on each frame of the mc pages) as page0, page1 etc. and then can control their timeline with this code (just replace the functions listed in the question above):
function f_nextFrame():void
{
var temp:uint = pageHolder.numChildren - 1;
var frame:uint = ((pageHolder.getChildAt(temp) as MovieClip)["page" + targetIDnew].currentFrame);
(pageHolder.getChildAt(temp) as MovieClip)["page" + targetIDnew].gotoAndStop(frame + 1);
}
function f_prevFrame():void
{
var temp:uint = pageHolder.numChildren - 1;
var frame:uint = ((pageHolder.getChildAt(temp) as MovieClip)["page" + targetIDnew].currentFrame);
(pageHolder.getChildAt(temp) as MovieClip)["page" + targetIDnew].gotoAndStop(frame - 1);
}