-
Creator 版本:3.6.1
-
目标平台: 谷歌游览器
-
重现方式:动态生成的列表,触控改变分辨率无法监听到
-
之前哪个版本是正常的: 100%
import {
_decorator,
Component,
Node,
instantiate,
view,
Sprite,
Color,
Label,
Prefab,
v3,
Vec3,
UITransform,
Quat,
rect,
Vec2,
UITransformComponent,
} from “cc”;
const { ccclass, property } = _decorator;
import { AttackEvent } from “./AttackEvent”;
const gameCanvas = view.getVisibleSize();
const gameWidthX = gameCanvas.width;
let x = 0,
line = 0,
itemLength = 12;
const map = Array.from(Array(itemLength), (v, k) => {
return {
id: k,
value: 10,
};
});
@ccclass(“itemList”)
export class itemList extends Component {
@property({ type: Node })
private target: Node = null;
@property({
type: Prefab,
displayName: “炮弹预制体”,
tooltip: “炮弹的预制体,飞机每时每刻都在发射炮弹”,
})
bullet: Prefab = null;
@property({
displayName: “是否可以发射炮弹”,
tooltip: “是否可以发射炮弹,为false时飞机将不在再射炮弹”,
})
is_fire: boolean = true;
@property(Node)
canvas: Node = null;
shellNums: number = 0;
nodes: Node[] = [];
start() {
if (!this.node) return;
this.touchEvent();
AttackEvent.Instance.init(this);
map.forEach((itemData, index) => {
let node = instantiate(this.target);
this.node.addChild(node);
if (x + 150 > gameWidthX) {
line += 1;
x = 0;
}
node.setPosition(x, -line * 180);
x += 170;
let sprite = node.getComponent(Sprite);
this.nodes = […this.nodes, node];
if (sprite) {
if (itemData.id === 0) {
this.nodeNumAdd(node);
} else {
sprite.color = new Color(101, 182, 135);
}
}
});
}
touchEvent() {
this.canvas.on(
Node.EventType.TOUCH_START,
(event) => {
console.log(“node”, event.getLocation());
this.touchEventNode(event, “start”);
},
this.canvas
);
this.canvas.on(
Node.EventType.TOUCH_END,
(event) => {
this.touchEventNode(event, “end”);
},
this.canvas
);
}
touchEventNode(event: any, type: “start” | “end”) {
const vue3 = new Vec3(event.getLocation().x, event.getLocation().y, 0);
const aa = this.node
.getComponent(UITransformComponent)
.convertToNodeSpaceAR(vue3);
const vue2 = new Vec2(aa.x, aa.y);
this.nodes.forEach((node, index) => {
if (
rect(
this.nodes[index].getComponent(UITransformComponent).getBoundingBox()
).contains(vue2)
) {
let sprite = node.getComponent(Sprite);
if (type === “start”) {
AttackEvent.Instance.onCurrentNode(node);
sprite.color = new Color(222, 222, 222);
} else {
AttackEvent.Instance.onTargetNode(node);
sprite.color = new Color(138, 180, 248);
}
}
});
}
nodeNumAdd(node: Node) {
const label = node.getComponentInChildren(Label);
setInterval(() => {
label.string = (parseInt(label.string) + 1).toString();
}, 1000);
}
update(dt: number): void {}
// 生成炮弹函数,pos为以飞机锚点为原点建立坐标系时炮弹的位置
}