Skip to content

Commit

Permalink
v1.2.7 pass target in Action.run(fn), make Action.destroy() reverse c…
Browse files Browse the repository at this point in the history
…onsistent
  • Loading branch information
reececomo committed Mar 3, 2025
1 parent 791e16b commit 7f29d99
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion dist/index.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.cjs.map

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,15 @@ declare abstract class _ extends Action {
* This action is reversible. The reversed action is equivalent to hide().
*/
static unhide(): Action;
/**
* Creates an action that removes all internal references, listeners and actions,
* as well as removes children from the display list.
*
* This action has an instantaneous duration.
*
* This action is not reversible; the reverse of this action is the same action.
*/
static destroy(options?: DestroyOptions): Action;
/**
* Creates an action that removes the node from its parent.
*
Expand All @@ -556,15 +565,15 @@ declare abstract class _ extends Action {
* This action is reversible; it tells the child to execute the reverse of the action specified by
* the action parameter.
*/
static runOnChild(nameOrLabel: string, action: Action): Action;
static runOnChild(childLabel: string, action: Action): Action;
/**
* Creates an action that executes a block.
*
* This action takes place instantaneously.
*
* This action is not reversible; the reverse action executes the same block.
*/
static run(fn: () => void): Action;
static run(fn: (target: TargetNode) => void): Action;
/**
* Creates an action that executes a stepping function over its duration.
*
Expand Down Expand Up @@ -633,6 +642,7 @@ export interface IActionTicker {
tick(deltaTime: number): number;
reset(): void;
}
export type DestroyOptions = Parameters<TargetNode["destroy"]>[0];
/**
* Any timing mode function.
*
Expand Down
2 changes: 1 addition & 1 deletion dist/index.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.mjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pixijs-actions",
"version": "1.2.5",
"version": "1.2.7",
"author": "Reece Como <[email protected]>",
"authors": [
"Reece Como <[email protected]>",
Expand Down
23 changes: 18 additions & 5 deletions src/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
GroupAction,
MoveByAction,
MoveToAction,
RemoveFromParentAction,
RepeatAction,
RepeatForeverAction,
RotateByAction,
Expand All @@ -32,6 +31,8 @@ import { Spritesheet, Texture } from 'pixi.js';

const DEG_TO_RAD = Math.PI / 180;

type DestroyOptions = Parameters<TargetNode["destroy"]>[0];

/**
* Create, configure, and run actions in PixiJS.
*
Expand Down Expand Up @@ -561,6 +562,18 @@ export abstract class _ extends Action {
return new SetVisibleAction(true);
}

/**
* Creates an action that removes all internal references, listeners and actions,
* as well as removes children from the display list.
*
* This action has an instantaneous duration.
*
* This action is not reversible; the reverse of this action is the same action.
*/
public static destroy(options?: DestroyOptions): Action {
return this.run(target => target.destroy(options));
}

/**
* Creates an action that removes the node from its parent.
*
Expand All @@ -569,7 +582,7 @@ export abstract class _ extends Action {
* This action is not reversible; the reverse of this action is the same action.
*/
public static removeFromParent(): Action {
return new RemoveFromParentAction();
return this.run(target => target.parent?.removeChild(target));
}

//
Expand All @@ -586,8 +599,8 @@ export abstract class _ extends Action {
* This action is reversible; it tells the child to execute the reverse of the action specified by
* the action parameter.
*/
public static runOnChild(nameOrLabel: string, action: Action): Action {
return new RunOnChildAction(action, nameOrLabel);
public static runOnChild(childLabel: string, action: Action): Action {
return new RunOnChildAction(action, childLabel);
}

//
Expand All @@ -601,7 +614,7 @@ export abstract class _ extends Action {
*
* This action is not reversible; the reverse action executes the same block.
*/
public static run(fn: () => void): Action {
public static run(fn: (target: TargetNode) => void): Action {
return new RunBlockAction(fn);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { Action } from '../../lib/Action';
export class RunOnChildAction extends Action {
public constructor(
protected readonly action: Action,
protected readonly nameOrLabel: string,
protected readonly label: string,
) {
super(0);
}

public reversed(): Action {
return new RunOnChildAction(this.action.reversed(), this.nameOrLabel)
return new RunOnChildAction(this.action.reversed(), this.label)
.setTimingMode(this.timingMode)
.setSpeed(this.speed);
}
Expand All @@ -22,18 +22,18 @@ export class RunOnChildAction extends Action {
let child: any;

if ('getChildByLabel' in target as any) {
child = (target as any).getChildByLabel(this.nameOrLabel); // PixiJS v8
child = (target as any).getChildByLabel(this.label); // pixi.js V8+
}
else {
child = target.children
.find((child: any) => child.label === this.nameOrLabel || child.name === this.nameOrLabel);
.find((child: any) => child.label === this.label || child.name === this.label);
}

if (child) {
child.run(this.action);
return;
}

throw new ReferenceError(`The target did not have a child matching '${this.nameOrLabel}'.`);
throw new ReferenceError(`The target did not have a child matching '${this.label}'.`);
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './RemoveFromParentAction';
export * from './RunOnChildAction';
export * from './SetVisibleAction';
6 changes: 3 additions & 3 deletions src/actions/custom/RunBlockAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Action } from '../../lib/Action';

export class RunBlockAction extends Action {
public constructor(
protected readonly block: () => void
protected readonly block: (target: TargetNode) => void
) {
super(0);
}
Expand All @@ -11,7 +11,7 @@ export class RunBlockAction extends Action {
return this;
}

protected onTick(): void {
this.block();
protected onTick(target: TargetNode): void {
this.block(target);
}
}
15 changes: 0 additions & 15 deletions src/actions/display-object/RemoveFromParentAction.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from './chainable';
export * from './custom';
export * from './delay';
export * from './display-object';
export * from './container';
export * from './follow-path';
export * from './move';
export * from './rotate';
Expand Down
3 changes: 1 addition & 2 deletions src/lib/ActionTicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ export class ActionTicker {
const target = this.target;
const action = this.action;

// If action no longer valid, or target not on the stage
// we garbage collect its actions.
// If action no longer valid, we garbage collect its runners.
if (target == null || target.destroyed) {
ActionTicker._removeActionTicker(this);

Expand Down

0 comments on commit 7f29d99

Please sign in to comment.