[LayaAirIDE3]2d相机打包后问题


ac82d919636ac11eeea8f225b13cacbc.png
图二为ide,图二为打包为安卓app,我使用了2d相机,并且也设置了limit的四个方向的范围,我在ide上运行,角色可以走到边界并且不露出limit范围以外的区域,但是打包成app安装到手机上,就会如
a934b80309cb956720b4a1d0f063ebda.jpg
图一所示会露出limit以外的区域,这是bug还是我哪里设置有问题呢?
 
已邀请:

LayaAir小牛

赞同来自:

可以发一个demo吗?简单一点的就好

1756447350用户

赞同来自:

main代码
// Main.ts - 简化版异步加载
import { CameraController } from "./CameraController";
import { Joystick } from "./Joystick";
import { Player } from "./Player"; 
const { regClass, property } = Laya; 
@regClass()
export class Main extends Laya.Script {
    @property({ type: Laya.Sprite })
    public player: Laya.Sprite; 
    @property({ type: Laya.Sprite })
    public joystick: Laya.Sprite; 
    @property({ type: Laya.Sprite })
    public map: Laya.Sprite; 
    @property({ type: Laya.Sprite })
    public camera: Laya.Sprite; 
    @property({ type: Laya.Sprite })
    public building: Laya.Sprite; 
    private playerScript: Player;
    private joystickScript: Joystick;
    private cameraController: CameraController; 
    onStart() {
        // 初始化组件引用
        this.playerScript = this.player.getComponent(Player);
        this.joystickScript = this.joystick.getComponent(Joystick);
        this.cameraController = this.camera.getComponent(CameraController); 
        // 设置边界参数
        const MAP_WIDTH = 6294;
        const MAP_HEIGHT = 4196;
        const MAP_X = -1230;
        const MAP_Y = -849; 
        this.playerScript.mapMinX = MAP_X;
        this.playerScript.mapMaxX = MAP_X + MAP_WIDTH;
        this.playerScript.mapMinY = MAP_Y;
        this.playerScript.mapMaxY = MAP_Y + MAP_HEIGHT; 
        // 摇杆事件监听
        this.joystickScript.owner.on(Joystick.MOVE, this, (data: any) => {
            this.playerScript.move(data.direction, data.force);
        }); 
        this.joystickScript.owner.on(Joystick.END, this, () => {
            this.playerScript.stop();
        }); 
        // 可视化边界
        this.visualizeBoundaries(); 
        // 确保摇杆初始状态正确
        this.joystickScript.enable(); 
        // 异步加载建筑图片
        this.loadBuildingImage();
    } 
    //异步加载建筑图片
    private loadBuildingImage(): void {
        Laya.loader.load(
            "res/building.png", // 你的建筑图片路径
            Laya.Handler.create(this, () => {
                // 加载完成后绘制到building Sprite上
                let texture = Laya.loader.getRes("res/building.png");
                this.building.graphics.drawTexture(texture, 0, 0);
            }),
            null,
            Laya.Loader.IMAGE
        );
    } 
    // 可视化边界
    private visualizeBoundaries() {
        const graphics = new Laya.Sprite();
        this.owner.addChild(graphics);
        this.addBoundaryLabels();
    } 
    private addBoundaryLabels() {
        const minXLabel = new Laya.Text();
        minXLabel.text = `minX: ${this.playerScript.mapMinX}`;
        minXLabel.color = "#FFFFFF";
        minXLabel.pos(this.playerScript.mapMinX, this.playerScript.mapMinY - 20);
        this.owner.addChild(minXLabel); 
        const maxXLabel = new Laya.Text();
        maxXLabel.text = `maxX: ${this.playerScript.mapMaxX}`;
        maxXLabel.color = "#FFFFFF";
        maxXLabel.pos(this.playerScript.mapMaxX - 100, this.playerScript.mapMinY - 20);
        this.owner.addChild(maxXLabel); 
        const minYLabel = new Laya.Text();
        minYLabel.text = `minY: ${this.playerScript.mapMinY}`;
        minYLabel.color = "#FFFFFF";
        minYLabel.pos(this.playerScript.mapMinX - 100, this.playerScript.mapMinY);
        this.owner.addChild(minYLabel); 
        const maxYLabel = new Laya.Text();
        maxYLabel.text = `maxY: ${this.playerScript.mapMaxY}`;
        maxYLabel.color = "#FFFFFF";
        maxYLabel.pos(this.playerScript.mapMinX - 100, this.playerScript.mapMaxY);
        this.owner.addChild(maxYLabel);
    }
} joystick代码
// Joystick.ts
const { regClass } = Laya; 
@regClass()
export class Joystick extends Laya.Script {
    public static MOVE: string = "JoystickMove";
    public static END: string = "JoystickEnd"; 
    private bg: Laya.Sprite;
    private knob: Laya.Sprite;
    private maxRadius: number = 240;
    private originPoint: Laya.Point;
    private touchId: number = -1;
    private isTouching: boolean = false;
    private deadZone: number = 45;
    private isEnabled: boolean = true; 
    onAwake() {
        this.createJoystick();
        this.bindEvents();
       
        // 监听全局事件
        Laya.stage.on("DIALOG_SHOW", this, this.disable);
        Laya.stage.on("DIALOG_HIDE", this, this.enable);
    } 
    onDestroy() {
        // 移除事件监听
        Laya.stage.off("DIALOG_SHOW", this, this.disable);
        Laya.stage.off("DIALOG_HIDE", this, this.enable);
    } 
    private createJoystick() {
        const owner = this.owner as Laya.Sprite;
        owner.size(420, 420);
        owner.mouseEnabled = true; 
        this.bg = new Laya.Sprite();
        this.bg.graphics.drawCircle(210, 210, 210, "#FFFFFF", 3);
        this.bg.alpha = 0.7;
        owner.addChild(this.bg); 
        this.knob = new Laya.Sprite();
        this.knob.graphics.drawCircle(0, 0, 105, "#FF5722");
        this.knob.pos(210, 210);
        owner.addChild(this.knob); 
        this.originPoint = new Laya.Point(210, 210);
    } 
    private bindEvents() {
        // 只在摇杆本身上监听事件
        const owner = this.owner as Laya.Sprite;
        owner.on(Laya.Event.MOUSE_DOWN, this, this.onTouchStart);
        Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this.onTouchMove);
        Laya.stage.on(Laya.Event.MOUSE_UP, this, this.onTouchEnd);
        Laya.stage.on(Laya.Event.MOUSE_OUT, this, this.onTouchEnd);
    } 
    private onTouchStart(e: Laya.Event) {
        // 如果摇杆被禁用,不处理任何事件
        if (!this.isEnabled) {
            e.stopPropagation();
            return;
        }
       
        // 检查点击是否在摇杆范围内
        const owner = this.owner as Laya.Sprite;
        const joystickPos = this.getJoystickGlobalPosition();
        const distance = Math.sqrt(
            Math.pow(e.stageX - (joystickPos.x + 210), 2) +
            Math.pow(e.stageY - (joystickPos.y + 210), 2)  
        ); 
        if (distance > 300) return; 
        console.log("摇杆按下");
        this.touchId = e.touchId;
        this.isTouching = true;
        this.onTouchMove(e); 
        // 阻止事件冒泡
        e.stopPropagation();
    } 
    private onTouchMove(e: Laya.Event) {
        // 如果摇杆被禁用,不处理任何事件
        if (!this.isEnabled) {
            e.stopPropagation();
            return;
        }
       
        if (!this.isTouching || this.touchId !== e.touchId) return;
       
        const joystickPos = this.getJoystickGlobalPosition();
        const centerX = joystickPos.x + 210;
        const centerY = joystickPos.y + 210; 
        const deltaX = e.stageX - centerX;
        const deltaY = e.stageY - centerY;
        const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY); 
        let targetX = 210;
        let targetY = 210;
        let dirX = 0;
        let dirY = 0;
        let force = 0; 
        if (distance > 0) {
            const normX = deltaX / distance;
            const normY = deltaY / distance;
            const actualDistance = Math.min(distance, this.maxRadius); 
            targetX = 210 + normX * actualDistance;
            targetY = 210 + normY * actualDistance;
            this.knob.pos(targetX, targetY); 
            if (distance > this.deadZone) {
                dirX = normX;
                dirY = normY;
                force = Math.min(1, actualDistance / this.maxRadius);
            }
        } 
        this.owner.event(Joystick.MOVE, {
            direction: new Laya.Point(dirX, dirY),
            force
        }); 
        // 阻止事件冒泡
        e.stopPropagation();
    } 
    private onTouchEnd(e: Laya.Event) {
        // 如果摇杆被禁用,不处理任何事件
        if (!this.isEnabled) {
            e.stopPropagation();
            return;
        }
       
        if (!this.isTouching || this.touchId !== e.touchId) return;
        this.resetJoystick(); 
        // 阻止事件冒泡
        e.stopPropagation();
    } 
    private resetJoystick() {
        Laya.Tween.to(this.knob, { x: 210, y: 210 }, 150); // 原始70 * 3 = 210
        this.owner.event(Joystick.END);
        this.touchId = -1;
        this.isTouching = false;
    } 
    private getJoystickGlobalPosition(): Laya.Point {
        const owner = this.owner as Laya.Sprite;
        return owner.localToGlobal(new Laya.Point(0, 0));
    }
   
    // 新增:启用摇杆
    public enable(): void {
        this.isEnabled = true;
        const owner = this.owner as Laya.Sprite;
        owner.mouseEnabled = true;
        owner.alpha = 1;
        console.log("摇杆已启用");
    }
   
    // 新增:禁用摇杆
    public disable(): void {
        this.isEnabled = false;
        const owner = this.owner as Laya.Sprite;
        owner.mouseEnabled = false;
        owner.alpha = 0.5;
        this.resetJoystick();
        console.log("摇杆已禁用");
    }
}

要回复问题请先

商务合作
商务合作