[Basic Type](../groups/Basic Type.Basic Type.md) / Action
Action Class
agent
Proxy is a design mode that allows you to create an intermediate object to control access to another object.
How does an agent work?
The editor has encapsulated the proxy function, which may be different from the proxy method you used before, but this method may be more convenient for you.
add Method to add specific transactions that require proxy.
call Method execution proxy.
Why use a proxy?
Agents can act as gatekeepers to control and restrict access to objects. It can verify the authority of the caller, restrict access to sensitive operations, or record access log. Proxy mode allows you to enhance the security of the original object without change it.
The proxy object can be responsible for create or initializing the real object on the first access, and redirecting subsequent access to the create object.
wait...
How to understand agents in a simple and understandable way?
Assuming you are a busy company owner, you need to handle many tasks, including communicating with clients, signing documents, and so on. However, due to your limited time, you are unable to personally handle all the affairs. So you decided to hire an assistant to help you handle these affairs. This assistant is your agent.
The agent (assistant) has the same ability and responsibility as you. He can communicate with customers and sign file on your behalf. When there is a transaction to be processed, you delegate the task to an agent, who will perform related operations on your behalf. However, agents do not completely replace your existence. Although agents can handle most transactions, in some cases, they need to delegate tasks to you for processing. For example, when encountering special requests or requiring your personal decisions, the agent will return the task to you.
Example usage: Create a script called ActionExample, place it in the object bar, open the script, enter the following code to save, run the game, press keyboard "1" to see the effect of the agent being called, press keyboard "2" to see the effect of the agent being removed, the code is as follows:
@Component
export default class ActionExample extends Script {
private readonly action:Action = new Action();
private readonly action1:Action1<player> = new Action1();
private readonly action2:Action2<number, player> = new Action2();
protected onStart(): void {
// Add listening for Action
this.action.add(() => {
console.log("action");
});
// Add listening for Action1
const id = this.action1.add((player: player) => {
if(player.age < 18) {
console.log("sorry , only those over 18 years old can enter")
} else {
// Specific achieve logic can be developed for the player
player.game();
}
});
// Add monitoring for Action2
this.action2.add(this.onAction2);
// Press key 1 on the keyboard to trigger each action
InputUtil.onKeyDown(Keys.One, () => {
this.action.call();
let playerOne : multiPlayer = new multiPlayer(10.5,"Janny");
this.action1.call(playerOne);
this.action2.call(2,playerOne);
});
}
// The listening function of Action2
private onAction2(num: number, player: player) {
console.log("action2");
player.game();
}
}
class player
{
public age:number = 2;
public name:string = "Li";
constructor(age: number, name: string) {
this.age = age;
this.name = name;
}
public game(){
console.log("player is playing game");
}
}
class lowPlayer extends player
{
public game(): void {
console.log("lowplayer is playing game");
}
}
class multiPlayer extends player
{
public game(): void {
console.log("multiPlayer is playing game");
}
}
@Component
export default class ActionExample extends Script {
private readonly action:Action = new Action();
private readonly action1:Action1<player> = new Action1();
private readonly action2:Action2<number, player> = new Action2();
protected onStart(): void {
// Add listening for Action
this.action.add(() => {
console.log("action");
});
// Add listening for Action1
const id = this.action1.add((player: player) => {
if(player.age < 18) {
console.log("sorry , only those over 18 years old can enter")
} else {
// Specific achieve logic can be developed for the player
player.game();
}
});
// Add monitoring for Action2
this.action2.add(this.onAction2);
// Press key 1 on the keyboard to trigger each action
InputUtil.onKeyDown(Keys.One, () => {
this.action.call();
let playerOne : multiPlayer = new multiPlayer(10.5,"Janny");
this.action1.call(playerOne);
this.action2.call(2,playerOne);
});
}
// The listening function of Action2
private onAction2(num: number, player: player) {
console.log("action2");
player.game();
}
}
class player
{
public age:number = 2;
public name:string = "Li";
constructor(age: number, name: string) {
this.age = age;
this.name = name;
}
public game(){
console.log("player is playing game");
}
}
class lowPlayer extends player
{
public game(): void {
console.log("lowplayer is playing game");
}
}
class multiPlayer extends player
{
public game(): void {
console.log("multiPlayer is playing game");
}
}
Hierarchy
Table of contents
Accessors
count(): number other |
---|
The number of monitoring methods |
Methods
add(fn : Function , thisArg? : any ): number other |
---|
Add a listening method |
call(...params : any ): void other |
implement |
clear(): void other |
Clear all listening |
includes(fn : Function , thisArg : any ): boolean other |
Determine whether a certain listening method is included |
remove(fn : number Function , thisArg? : any ): void other |
Remove a listening method |
Accessors
count
• |
---|
The number of monitoring methods Returns |
number |
---|
Methods
add
• add(fn
, thisArg?
): number
other
Add a listening method
Parameters
fn Function | method |
---|---|
thisArg? any | Domain, the following example can explain the concept of domain default: undefined |
Returns
number | The unique identifier for this monitoring can be used for removal. If -1 is returned, it indicates that this method has been added before and will not be added again. |
---|
Usage example: Explain the concept of "domain"
@Component
export default class ActionExample extends Script {
private readonly action1: Action1<player> = new Action1();
private readonly action2: Action1<player> = new Action1();
private readonly action3: Action1<player> = new Action1();
private readonly action4: Action1<player> = new Action1();
private readonly action5: Action1<player> = new Action1();
public playerOne: multiPlayer = new multiPlayer(10.5, "Janny");
public playertwo: lowPlayer = new lowPlayer(12, "Danny");
public playerthree : multiPlayer = new multiPlayer(11,"Liming");
private thisarg: number = 0;
protected onStart(): void {
// The domain is passed into the lowPlayer object, which retrieves the value of thisarg from lowPlayer.
this.action1.add(this.clickOne, this.playertwo);
// Passing this into the domain is equivalent to bind (this), and the value of this class's thisarg is the same.
this.action2.add(this.clickOne, this);
this.action3.add(this.clickOne.bind(this));
// Domain doesn't transmit anything, it's undefined
this.action4.add(this.clickOne);
// The domain is passed into a multiPlayer object, which retrieves the value of thisarg from multiPlayer.
this.action5.add(this.clickOne, this.playerthree);
InputUtil.onKeyDown(Keys.One, () => {
console.log("1")
this.action1.call(this.playerOne);
});
InputUtil.onKeyDown(Keys.Two, () => {
console.log("2")
this.action2.call(this.playerOne);
});
InputUtil.onKeyDown(Keys.Three, () => {
console.log("3")
this.action3.call(this.playerOne);
});
InputUtil.onKeyDown(Keys.Four, () => {
console.log("4")
this.action4.call(this.playerOne);
});
InputUtil.onKeyDown(Keys.Five, () => {
console.log("5")
this.action5.call(this.playerOne);
});
}
private clickOne(player: player): void {
console.warn("action1 is called: " + this.thisarg);
player.game();
}
}
class player {
public age: number = 20;
public name: string = "Li";
constructor(age: number, name: string) {
this.age = age;
this.name = name;
}
public game() {
console.log("player is playing game");
}
}
class lowPlayer extends player {
private thisarg: number = 10;
public game(): void {
console.log("lowplayer is playing game");
console.log(this.age + " " + this.name);
}
}
class multiPlayer extends player {
private thisarg: number = 20;
public game(): void {
console.log("multiPlayer is playing game");
console.log(this.age + " " + this.name);
}
}
@Component
export default class ActionExample extends Script {
private readonly action1: Action1<player> = new Action1();
private readonly action2: Action1<player> = new Action1();
private readonly action3: Action1<player> = new Action1();
private readonly action4: Action1<player> = new Action1();
private readonly action5: Action1<player> = new Action1();
public playerOne: multiPlayer = new multiPlayer(10.5, "Janny");
public playertwo: lowPlayer = new lowPlayer(12, "Danny");
public playerthree : multiPlayer = new multiPlayer(11,"Liming");
private thisarg: number = 0;
protected onStart(): void {
// The domain is passed into the lowPlayer object, which retrieves the value of thisarg from lowPlayer.
this.action1.add(this.clickOne, this.playertwo);
// Passing this into the domain is equivalent to bind (this), and the value of this class's thisarg is the same.
this.action2.add(this.clickOne, this);
this.action3.add(this.clickOne.bind(this));
// Domain doesn't transmit anything, it's undefined
this.action4.add(this.clickOne);
// The domain is passed into a multiPlayer object, which retrieves the value of thisarg from multiPlayer.
this.action5.add(this.clickOne, this.playerthree);
InputUtil.onKeyDown(Keys.One, () => {
console.log("1")
this.action1.call(this.playerOne);
});
InputUtil.onKeyDown(Keys.Two, () => {
console.log("2")
this.action2.call(this.playerOne);
});
InputUtil.onKeyDown(Keys.Three, () => {
console.log("3")
this.action3.call(this.playerOne);
});
InputUtil.onKeyDown(Keys.Four, () => {
console.log("4")
this.action4.call(this.playerOne);
});
InputUtil.onKeyDown(Keys.Five, () => {
console.log("5")
this.action5.call(this.playerOne);
});
}
private clickOne(player: player): void {
console.warn("action1 is called: " + this.thisarg);
player.game();
}
}
class player {
public age: number = 20;
public name: string = "Li";
constructor(age: number, name: string) {
this.age = age;
this.name = name;
}
public game() {
console.log("player is playing game");
}
}
class lowPlayer extends player {
private thisarg: number = 10;
public game(): void {
console.log("lowplayer is playing game");
console.log(this.age + " " + this.name);
}
}
class multiPlayer extends player {
private thisarg: number = 20;
public game(): void {
console.log("multiPlayer is playing game");
console.log(this.age + " " + this.name);
}
}
call
• call(...params
): void
other
implement
Parameters
...params any | Parameter sequence |
---|
Example usage: Create a script called ActionExample, place it in the object bar, open the script, enter the following code to save, run the game, press keyboard "1" to see the effect of the agent being called, press keyboard "2" to see the effect of the agent being removed, the code is as follows:
@Component
export default class ActionExample extends Script {
private readonly action:Action = new Action();
private readonly action1:Action1<number> = new Action1();
private readonly action2:Action2<number, string> = new Action2();
protected onStart(): void {
// Add listening for Action
this.action.add(() => {
console.log("action");
});
// Add listening for Action1
const id = this.action1.add((num: number) => {
console.log("onAction1", num);
});
// Add monitoring for Action2
this.action2.add(this.onAction2, this);
// Press key 1 on the keyboard to trigger each action
InputUtil.onKeyDown(Keys.One, () => {
this.action.call();
this.action1.call(1);
this.action2.call(2, "testString");
});
// Press the 2nd key on the keyboard to remove the listening of each Action, and triggering it again after removal will not execute
InputUtil.onKeyDown(Keys.Two, () => {
this.action1.remove(id);
this.action2.remove(this.onAction2, this);
});
}
// The listening function of Action2
private onAction2(num: number, str: string) {
console.log("onAction2", num, str);
}
}
@Component
export default class ActionExample extends Script {
private readonly action:Action = new Action();
private readonly action1:Action1<number> = new Action1();
private readonly action2:Action2<number, string> = new Action2();
protected onStart(): void {
// Add listening for Action
this.action.add(() => {
console.log("action");
});
// Add listening for Action1
const id = this.action1.add((num: number) => {
console.log("onAction1", num);
});
// Add monitoring for Action2
this.action2.add(this.onAction2, this);
// Press key 1 on the keyboard to trigger each action
InputUtil.onKeyDown(Keys.One, () => {
this.action.call();
this.action1.call(1);
this.action2.call(2, "testString");
});
// Press the 2nd key on the keyboard to remove the listening of each Action, and triggering it again after removal will not execute
InputUtil.onKeyDown(Keys.Two, () => {
this.action1.remove(id);
this.action2.remove(this.onAction2, this);
});
}
// The listening function of Action2
private onAction2(num: number, str: string) {
console.log("onAction2", num, str);
}
}
clear
• clear(): void
other
Clear all listening
includes
• includes(fn
, thisArg
): boolean
other
Determine whether a certain listening method is included
Parameters
fn Function | method |
---|---|
thisArg any | field |
Returns
boolean | result |
---|
remove
• remove(fn
, thisArg?
): void
other
Remove a listening method
Parameters
fn number Function | Method Monitor unique identifier |
---|---|
thisArg? any | Domain default: undefined |